Is the Javascript date object always one day off?

后端 未结 23 2629
既然无缘
既然无缘 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

    Just want to add that apparently adding a space at the end of the string will use UTC for creation.

    new Date("2016-07-06")
    > Tue Jul 05 2016 17:00:00 GMT-0700 (Pacific Daylight Time)
    
    new Date("2016-07-06 ")
    > Wed Jul 06 2016 00:00:00 GMT-0700 (Pacific Daylight Time)
    

    Edit: This is not a recommended solution, just an alternative answer. Please do not use this approach since it is very unclear what is happening. There are a number of ways someone could refactor this accidentally causing a bug.

提交回复
热议问题