I am trying to convert an UTC date to millis since UNIX epoch.
For this, I am doing the following:
const date = new Date(); // local date
const utc = moment().utc(date); // UTC date
const utcMillis = utc.valueOf(); // Millis since UNIX epoch
Currently, I live in Spain (UTC 2), and I have noticed that
date.getTime() === moment().utc(date).valueOf()
Why?
Note: you can test this online here
CodePudding user response:
Date()
doesn't value to local date and time, but values to UTC time instead. Check the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
So, it makes total sense for both values to be equal.
CodePudding user response:
It doesn't matter what the timezone of a date is as it only represents a specific point in time. Given that all dates/times are based on the number of milliseconds since 1970-01-01T00:00:00Z then it makes sense that no matter what you do, so long as 2 dates represent the same moment in time then their underlying values should also be the same.