UTC到本地的日期和时间 [英] UTC date and time to local

查看:95
本文介绍了UTC到本地的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下功能将UTC时间转换为本地时间。它

适用于GMT-(减号)时区但是它为GMT +(加号)时区提供了不正确的

结果?


//从UTC格式化为当地时间

function formatToLocalTimeDate(inDate){

var today = new Date();

var inDateMod = new Date(inDate);

offSet = today.getTimezoneOffset();

if(offSet< 0){

inDateMod.setMinutes (inDateMod.getMinutes()+ offSet);

} else {

inDateMod.setMinutes(inDateMod.getMinutes() - offSet);

}

返回inDateMod;

}


任何人都可以帮忙吗?或者有没有人有一个代码可以为我做这个吗?

谢谢

Maz。

Hi,

I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?
Thanks
Maz.

推荐答案

< ma ******* @ gmail.com>在消息中写道

news:11 ********************** @ v46g2000cwv.googlegr oups.com ...
<ma*******@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...


我有以下功能将UTC时间转换为本地时间。它适用于GMT-(减号)时区但是它为GMT +(加号)时区提供了错误的结果?

//格式化为当地时间UTC var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if (offSet< 0){
inDateMod.setMinutes(inDateMod.getMinutes()+ offSet);
} else {
inDateMod.setMinutes(inDateMod.getMinutes() - offSet);
}
返回inDateMod;
}

任何人都可以帮忙吗?或者有人有代码可以为我做这个吗?
谢谢
Maz。
Hi,

I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?
Thanks
Maz.




看看

dateObj.setUTCHours(hh,mm)方法 -

如果设置了这些,那么(本地)时间将自动设置。

At至少它在Firefox中,我刚测试过它。
http://developer.mozilla.org/en/docs...s:Date#Methods

希望这会有所帮助



Take a look at the
dateObj.setUTCHours(hh,mm) method -
If you set those, then the (local) time will be set automagically.
At least it does in Firefox, where I just tested it.
http://developer.mozilla.org/en/docs...s:Date#Methods
Hope this helps


ma*******@gmail.com 写道:
ma*******@gmail.com wrote:
我有以下函数将UTC时间转换为本地时间。它适用于GMT-(减号)时区但是它为GMT +(加号)时区提供了错误的结果?

//格式化为当地时间UTC var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if (offSet< 0){
inDateMod.setMinutes(inDateMod.getMinutes()+ offSet);
} else {
inDateMod.setMinutes(inDateMod.getMinutes() - offSet);
I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);




Date.prototype.getMinutes()已经在

当地时间返回分钟。


如果你真的想从UTC转换为本地时间(这完全是冗余的
),你需要使用

Date.prototype.getUTCMinutes()来检索值。 。

PointedEars



Date.prototype.getMinutes() already returns the minutes in the
local time.

If you really wanted to convert from UTC to local time (which is
entirely redundant), you would need to retrieve the value with
Date.prototype.getUTCMinutes() instead.
PointedEars


JRS:文章< 11 ****************** ****@v46g2000cwv.g ooglegroups .com>

,日期为2006年4月8日星期六22:00:21远程,见于

news:comp.lang.javascript, ma ******* @ gmail.com 发布:
JRS: In article <11**********************@v46g2000cwv.googlegroups .com>
, dated Sat, 8 Apr 2006 22:00:21 remote, seen in
news:comp.lang.javascript, ma*******@gmail.com posted :
我有以下功能将UTC时间转换为本地时间。它适用于GMT-(减号)时区但是它为GMT +(加号)时区提供了错误的结果?

//格式化为当地时间UTC var today = new Date();
var inDateMod = new Date(inDate);


低效,如果inDate作为日期对象到达:使用

var inDateMod = new Date(+ inDate);

to复制Date对象的值。

offSet = today.getTimezoneOffset();
if(offSet< 0){
inDateMod.setMinutes(inDateMod.getMinutes()+ offSet);
} else {
inDateMod.setMinutes(inDateMod.getMinutes() - offSet);
}
返回inDateMod;
}
I have the following function to convert UTC time to Local time. It
works perfect for GMT- (Minus) time zones however it provides incorrect
results for GMT+(Plus) time zones?

// Format to local time from UTC
function formatToLocalTimeDate(inDate) {
var today = new Date();
var inDateMod = new Date(inDate);
Inefficient, if inDate arrives as a Date Object : use
var inDateMod = new Date(+inDate);
to copy the value of a Date Object.
offSet = today.getTimezoneOffset();
if(offSet < 0) {
inDateMod.setMinutes(inDateMod.getMinutes()+offSet );
} else {
inDateMod.setMinutes(inDateMod.getMinutes()-offSet);
}
return inDateMod;
}

Can anyone help with this? Or does anyone have a code that would do
this for me?




任何人认为适当添加负偏移并减去

正面值不值得复制,应该建议采取

编织而不是计算。


包括非当前偏移量当前时间似乎不太可能是有用的。


请注意,使用setMinutes()越过夏令时更改可能不会
给出预期的结果。


我目前唯一可以做出类似事情的原因就是在处理别人的当地时间时。但是,因为它没有你所声称的,并且没有任何意义,所以很难看出你做了什么需要。

inDateMod =新日期(+ inDate + - 6e4 * new Date()。getTimezoneOffset())


应该做的很多代码的程序员想到的,但更多

有效。


阅读新闻组常见问题解答;见下文。


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/> JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,常见问题项目,链接。



Anyone who thinks it appropriate to add negative offsets and subtract
positive ones is not worth copying from, and should be advised to take
up knitting instead of computing.

Including the current offset in a non-current time seems unlikely to be
useful.

Be aware that crossing a Summer Time change with setMinutes() may not
give the expected result.

The only reason I can at present see for doing something like that would
be when dealing with someone else''s local time. But as it does not do
what you claim, and neither makes sense, it''s hard to see what you do
need.

inDateMod = new Date(+inDate +- 6e4*new Date().getTimezoneOffset())

should do much what your code''s programmer was thinking of, but more
efficiently.

Read the newsgroup FAQ; see below.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


这篇关于UTC到本地的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆