Oh Javascript... 🙄

Kasey Speakman - Jun 17 '18 - - Dev Community

Just ran across this little gem.

new Date('2018-01-01')
<- Sun Dec 31 2017 18:00:00 GMT-0600 (Central Standard Time)

new Date('2018/01/01')
<- Mon Jan 01 2018 00:00:00 GMT-0600 (Central Standard Time) 
Enter fullscreen mode Exit fullscreen mode

The reason this happens (I think) is because JS parses the first date as ISO 8601 format. And since no timezone offset is specified it assumes UTC. However, JavaScript will only create Dates in local time. So after applying the local time zone, it is 6pm the previous day.

The second one uses a / separator, so it does not trigger the ISO parse branch. Instead it sensibly presumes I am interested in a date in the current time zone.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player