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

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

问题描述

我有一个倒计时时钟,设置为 2014 年 1 月 1 日早上 8 点倒计时.

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

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

这有效,但我想更进一步并将其设置为特定时区.就我而言,UTC -7.

我已经阅读了这个答案,其中说要使用:

new Date(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 日中午的日期.提醒日期将打印相当于 2013-12-30T17:00:00+05:00 的本地时间(假设 +5: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);警报(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天全站免登陆