javascript 使用 CET 时区创建一个 Date 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20867562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a Date object with CET timezone
提问by Nandish A
To create Date object in UTC, we would write
要在 UTC 中创建 Date 对象,我们会写
new Date(Date.UTC(2012,02,30));
Without Date.UTC, it takes the locale
and creates the Date
object. If I have to create a Date object for CET
running the program in some part of the world, how would I do it?
如果没有 Date.UTC,它将使用locale
并创建Date
对象。如果我必须创建一个 Date 对象来CET
在世界的某个地方运行程序,我该怎么做?
回答by T.J. Crowder
You don't create a JavaScript Date
object "in" any specific timezone. JavaScript Date
objects always work from a milliseconds-since-the-Epoch UTC value. They have methods that apply the local timezone offset and rules (getHours
as opposed to getUTCHours
), but only the local timezone. You can't setthe timezone the Date
object uses for its "local" methods.
您不会Date
在任何特定时区“中”创建 JavaScript对象。JavaScriptDate
对象总是从纪元以来的毫秒 UTC 值开始工作。他们有应用本地时区偏移和规则(getHours
与 相对getUTCHours
)的方法,但仅适用于本地时区。您无法设置Date
对象用于其“本地”方法的时区。
What you're doing with Date.UTC
(correctly, other than the leading 0
on 02
) is just initializing the object with the appropriate milliseconds-since-the-Epoch value for that date/time (March 30th at midnight) in UTC, whereas new Date(2012, 2, 30)
would have interpreted it as March 30th at midnight local time. There is nodifference in the Date
object other than the datetime it was initialized with.
什么你和做Date.UTC
(正确的,比其他领先0
上02
)只是初始化与适当以来的毫秒数最大纪元对于UTC该日期/时间(3月30日午夜)值的对象,而new Date(2012, 2, 30)
将已经将它解释当地时间 3 月 30 日午夜。除了初始化的日期时间之外,对象没有任何区别Date
。
If you need a timezone other than local, all you can do is use the UTC version of Date
's functions and apply your own offset and rules for the timezone you want to use, which is non-trivial. (The offset is trivial; the rules tend not to be.)
如果您需要本地以外的时区,您所能做的就是使用Date
's 函数的 UTC 版本,并为要使用的时区应用您自己的偏移量和规则,这很重要。(偏移量是微不足道的;规则往往不是。)
If you go looking, you can find Node modules that handle timezones for you. A quick search for "node timezone" just now gave me timezone
as the first hit. It also gave me links to this SO question, this SO question, and this list of timezone modules for Node.
如果你去寻找,你可以找到为你处理时区的 Node 模块。刚刚快速搜索“节点时区”给了我timezone
第一次点击。它还为我提供了指向此 SO 问题、此 SO 问题以及Node 时区模块列表的链接。
回答by Sergey Krivtsov
function getCETorCESTDate() {
var localDate = new Date();
var utcOffset = localDate.getTimezoneOffset();
var cetOffset = utcOffset + 60;
var cestOffset = utcOffset + 120;
var cetOffsetInMilliseconds = cetOffset * 60 * 1000;
var cestOffsetInMilliseconds = cestOffset * 60 * 1000;
var cestDateStart = new Date();
var cestDateFinish = new Date();
var localDateTime = localDate.getTime();
var cestDateStartTime;
var cestDateFinishTime;
var result;
cestDateStart.setTime(Date.parse('29 March ' + localDate.getFullYear() + ' 02:00:00 GMT+0100'));
cestDateFinish.setTime(Date.parse('25 October ' + localDate.getFullYear() + ' 03:00:00 GMT+0200'));
cestDateStartTime = cestDateStart.getTime();
cestDateFinishTime = cestDateFinish.getTime();
if(localDateTime >= cestDateStartTime && localDateTime <= cestDateFinishTime) {
result = new Date(localDateTime + cestOffsetInMilliseconds);
} else {
result = new Date(localDateTime + cetOffsetInMilliseconds);
}
return result;
}