Does Javascript date toLocaleString account for DST?

后端 未结 3 826
礼貌的吻别
礼貌的吻别 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:16

    As you use "UTC" the date itself will be UTC format, but the toLocaleString() takes client's locale into account, which means it'll return the date in string updated with all and every changes typical to client's regional and locale settings (DST, date/time format, etc).
    As JS documentation describes this: "The toLocaleString() method converts a Date object to a string, using locale settings.".
    If you want to avoid this, use the toUTCString() method instead.

    I'd also recommend reading the accepted solution for the question Javascript dates: what is the best way to deal with Daylight Savings Time? to avoid (at least, to try to avoid :) future issues related to JS, browsers and locales.

    Hope this helps!

提交回复
热议问题