LD
Change your colour scheme

TIL: Why Date.parse gives unexpected results for GMT

Published:

TL;DR: Timezones are weird

Someone posted about this in a Discord server I’m on: Javascript’s Date object has a parse function, Date.parse that takes a string can converts it to a timestamp. If you give it a non-standard string, in this case Jan 1, 1970, it should still parse it.

The problem is though, that even though we’re in GMT, and GMT == UTC, Date.parse("Jan 1, 1970") results in a timestamp of -3600000, or Dec 31, 1969 23:00:00. If you explicitly add the GMT timezone, it’s fine.

It turns out that if you omit the timezone, Date.parse will use your system timezone - now in my case that’s GMT today[1], but if I were to go back to 1970 I’d actually find that I’d be in BST. This is because the government experimented with scrapping daylight savings. So, when you parse that specific date, your timezone is actually UTC+1.


  1. Obviously this only breaks in the UK ↩︎

Tags:

About the author

My face

I'm Lewis Dale, a software engineer and web developer based in the UK. I write about writing software, silly projects, and cycling. A lot of cycling. Too much, maybe.

Responses