Does Javascript date toLocaleString account for DST?

后端 未结 3 827
礼貌的吻别
礼貌的吻别 2021-01-24 00:46

I have set a deadline in UTC, as shown below, and I\'m wondering what exactly the toLocaleString() method will do to it on user\'s local machines. For instance, will it account

3条回答
  •  臣服心动
    2021-01-24 01:07

    In general, the answer is yes. JavaScript will represent the UTC value at the appropriate local time based on the time zone settings of the computer it is running on. This includes adjustment for DST. However, as others have pointed out, the details are implementation specific.

    If you want a consistent output, I would use a library to format your dates instead of relying on the default implementation. The best library (IMHO) for this is moment.js. The live examples on their main page will give you an idea of what it can do.

    UPDATE

    If you are passing UTC values that you want converted to the correct local time, and that time falls into a period where the time zone rules are different than the current one - then the results will be invalid. This is crazy, but true - and by design in the ECMA spec. Read - JavaScript Time Zone is wrong for past Daylight Saving Time transition rules

提交回复
热议问题