Reputation: 175
(Yes, these are older releases of Node, I'm in the process of updating an old codebase and am curious as to the reasons for this happening).
If I'm using Node 8 and I run new Date('1970-01-01 00:00:00')
, I get this date returned to me. If I run new Date('1972-01-01 00:00:00')
, I get that date returned to me.
However, if I upgrade to Node 10 and run new Date('1970-01-01 00:00:00')
, I get 1969-12-31T23:00:00.000Z
– an hour is being subtracted from the time, causing it to roll back to the previous year. If I run new Date('1972-01-01 00:00:00')
, however, I get back 1972-01-01T00:00:00.000Z
, without rolling back to the previous year.
It's currently BST here in the UK, which I initially thought could account for the issue, but I'd expect it to happen for all created dates, rather than just the year 1970 (and 1971, it turns out).
Any ideas?
Upvotes: 1
Views: 100
Reputation: 3647
Node tries to get the local timezone on first usage of Date. Therefor the parsing of dates can vary. See Difference in assumed time zone.
If you want node to assume every date you submit is a UTC-Date, try one of the following:
new Date("yyyy-mm-dd")
process.env.TZ = "UTC";
before accessing any Date() functionExample 1:
process.env.TZ = "GMT"; // or GMT+0 or UTC
console.log(Date.parse("1970-01-01")); // 0
console.log(Date.parse("1970-01-01 00:00:00")); // 0
Example 2:
process.env.TZ = "GMT+1"; // or Europe/London
console.log(Date.parse("1970-01-01")); // 0
console.log(Date.parse("1970-01-01 00:00:00")); // -3600000
Example 3:
process.env.TZ = "Europe/Berlin"; // or GMT+1
console.log(Date.parse("1970-01-01")); // 0
console.log(Date.parse("1970-01-01 00:00:00")); // -3600000
Upvotes: 1