简体   繁体   English

创建具有CET时区的Date对象

[英]Create a Date object with CET timezone

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. 如果没有Date.UTC,它将使用locale并创建Date对象。 If I have to create a Date object for CET running the program in some part of the world, how would I do it? 如果必须为在世界各地运行该程序的CET创建Date对象,该怎么办?

You don't create a JavaScript Date object "in" any specific timezone. 您不会在任何特定时区“中”创建JavaScript Date对象。 JavaScript Date objects always work from a milliseconds-since-the-Epoch UTC value. Date ETC以来,JavaScript Date对象始终以毫秒为单位工作。 They have methods that apply the local timezone offset and rules ( getHours as opposed to getUTCHours ), but only the local timezone. 它们具有应用本地时区偏移量和规则( getHours而不是getUTCHours )的方法,但仅应用本地时区。 You can't set the timezone the Date object uses for its "local" methods. 您不能设置 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. 您对Date.UTC所做的Date.UTC (正确的是,除了02的前导0以外)只是使用UTC中该日期/时间(3月30日午夜)使用适当的毫秒值(因为自Epoch值)初始化对象,而new Date(2012, 2, 30)会将其解释为当地时间3月30日午夜。 There is no difference in the Date object other than the datetime it was initialized with. 除初始化对象的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. 如果您需要的不是本地时区,则可以使用Date函数的UTC版本,并为您要使用的时区应用自己的偏移量和规则,这并非易事。 (The offset is trivial; the rules tend not to be.) (偏移量是微不足道的;规则并非如此。)

If you go looking, you can find Node modules that handle timezones for you. 如果您去寻找,可以找到可以为您处理时区的Node模块。 A quick search for "node timezone" just now gave me timezone as the first hit. 刚才对“节点时区”的快速搜索使我成为了timezone It also gave me links to this SO question , this SO question , and this list of timezone modules for Node . 它还给了我这个SO问题SO问题以及Node时区模块列表的链接

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM