Wrong unix time from parsed DateTime

Here is code

const dateStr = '1989-11-11T03:34';
let date = new Date(dateStr);
console.log(date.toUTCString());
console.log(+date);

And output:

Fri, 10 Nov 1989 22:34:00 GMT
626740440000 <--- far future

Why unix time is in far future?

Here is Playcode link

>Solution :

It’s not wrong.

You seem to have two issues with what you’re getting:

  1. The time isn’t the same in your toISOString output.
  2. The number you’re getting from +date isn’t what you expect.

But both are correct:

  1. The string has no timezone indicator on it, so since it’s a date+time string, it’s parsed as local time. This is covered by the Date Time String Format section of the specification. But your output is showing GMT (because toISOString uses GMT).
  2. 626740440000 is the number of milliseconds since The Epoch, not seconds as it was in the original Unix epoch scheme. This is covered by the Time Values and Time Range section of the specification.

If you want to parse your string as UTC (loosely, GMT), add a Z to the end of it indicating that’t is in GMT.

If you want the number of seconds since The Epoch, divide +date by 1000 (perhaps rounding or flooring the result).

Leave a Reply