Is the Javascript date object always one day off?

后端 未结 23 2330
既然无缘
既然无缘 2020-11-22 01:49

In my Java Script app I have the date stored in a format like so:

2011-09-24

Now when I try using the above value to create a new Date obje

23条回答
  •  无人共我
    2020-11-22 02:26

    You are using the ISO date string format which, according to this page, causes the date to be constructed using the UTC timezone:

    Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

    If you format the text differently, such as "Jan 01 1970", then (at least on my machine) it uses your local timezone.

提交回复
热议问题