如何在创建JavaScript日期时指定时区? [英] How do I specify the time zone when creating a JavaScript Date?

查看:87
本文介绍了如何在创建JavaScript日期时指定时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用以下代码设置日期:

我有一个倒计时时钟设置为倒计时到2014年1月1日上午8点。 >

  var futureDate = new Date(2014,0,1,8,0,0,0); 

这个工作,但我想进一步,并将其设置到特定的时区。在我的情况下,UTC -7。



我已阅读此答案使用说明:

 新日期(Date.UTC(年,月,日,小时,分钟,秒)) 

但是我很困惑的是我将时区设置为UTC -7,在线只让我感到困惑。



有人可以解释 Date.UTC 的工作原理,如何设置时区,以便我的倒计时时钟倒数基于指定的时区?



注意:任何答案都必须是客户端唯一的代码。

解决方案


有人可以解释Date.UTC如何工作


Date.UTC 创建提供的年,月,日等的时间值。没有任何抵消。所以如果客户机设置为UTC +05:00,那么:

  var d = new Date(Date。 UTC(2013,11,30,12,0,0)); 

将在格林威治创建相当于2013年12月30日中午的日期。提醒日期将打印当地时间(假设+5:00),相当于2013-12-30T17:00:00 + 05:00。


如何设置时区让我的倒计时时钟根据指定的时区倒计时?


你不能设置时区,但是您可以使用UTC值创建日期对象,调整偏移量的小时和分钟,然后使用UTC方法获取倒计时的日期和时间组件。



例如

  function z(n){return(n <10?'0' )+ n;} 

var d = new Date(Date.UTC(2012,11,30,12,0,0));

d.setUTCHours(d.getUTCHours() - 7);

alert(d.getUTCFullYear()+' - '+ z(d.getUTCMonth()+ 1)+' - '+
z(d.getUTCDate())+'T' + z(d.getUTCHours())+':'+
z(d.getUTCMinutes())+':'+ z(d.getUTCSeconds())+'-07:00'
) ;

// 2012-12-30T05:00:00-07:00

如果使用非UTC方法,则本地偏移将影响结果。


I have a countdown clock that is set to countdown to 8am on January 1, 2014.

I am using the following code to set the date:

var futureDate = new Date(2014, 0, 1, 8, 0, 0, 0);

This works but I would like to take it a step further and set it to a specific timezone. In my case UTC -7.

I have read this answer which says to use:

new Date(Date.UTC(year, month, day, hour, minute, second))

but what I am confused about is how I set the timezone to be UTC -7 and what I read online only leaves me more confused.

Can someone explain how Date.UTC works and how do I set a timezone so my countdown clock is counting down based on the specified timezone?

Note: Any answer must be client side only code.

解决方案

Can someone explain how Date.UTC works

Date.UTC creates a timevalue for the provided year, month, date, etc. without any offset. So if the client machine is set for, say, UTC +05:00 then:

var d = new Date(Date.UTC(2013, 11, 30, 12, 0, 0));

will create a date equivalent to noon on 30 December 2013 at Greenwich. Alerting the date will print a local time (assuming +5:00) equivalent to 2013-12-30T17:00:00+05:00.

and how do I set a timezone so my countdown clock is counting down based on the specified timezone?

You can't set the timezone, however you can use UTC values to create a date object, adjust the hours and minutes for the offset, then use the UTC methods to get the date and time components for the countdown.

e.g.

function z(n){return (n < 10? '0' : '') + n;}

var d = new Date(Date.UTC(2012, 11, 30, 12, 0, 0));

d.setUTCHours(d.getUTCHours() - 7);

alert(d.getUTCFullYear() + '-' + z(d.getUTCMonth() + 1) + '-' + 
      z(d.getUTCDate()) + 'T' + z(d.getUTCHours()) + ':' +
      z(d.getUTCMinutes()) + ':' + z(d.getUTCSeconds()) + '-07:00'
);

// 2012-12-30T05:00:00-07:00

If non–UTC methods are used, the local offset will affect the result.

这篇关于如何在创建JavaScript日期时指定时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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