Convert Javascript current date time to UTC date time in milliseconds

时间:2018-03-22 23:27:28

标签: javascript date datetime utc

I can't see a way to convert the current date/time in Javascript to a full date time in UTC. I need an equivalent to

var now = new Date().getTime();

but one that returns the UTC time in milliseconds

some thing like this

var UTCnow = new Date().getUTCTime();

I can't seem to see what I can use to make this happen in the JS Date object.

Any help would be appreciated.

1 个答案:

答案 0 :(得分:3)

Per the documentation:

The getTime() method returns the numeric value corresponding to the time for the specified date according to universal time.

getTime() always uses UTC for time representation. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone.

In other words, it already does what you are asking.

You can also do the same thing without instantiating a Date object:

var utcnow = Date.now();

Which the docs describe as:

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.