简体   繁体   English

以毫秒为单位将 Javascript 当前日期时间转换为 UTC 日期时间

[英]Convert Javascript current date time to UTC date time in milliseconds

I can't see a way to convert the current date/time in Javascript to a full date time in UTC.我看不到将 Javascript 中的当前日期/时间转换为 UTC 中的完整日期时间的方法。 I need an equivalent to我需要一个相当于

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

but one that returns the UTC time in milliseconds但是一个以毫秒为单位返回UTC时间

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.我似乎看不到在 JS Date 对象中可以使用什么来实现这一点。

Any help would be appreciated.任何帮助将不胜感激。

Per the documentation :根据文档

The getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. getTime()方法根据通用时间返回指定日期的时间对应的数值。

getTime() always uses UTC for time representation. getTime()始终使用 UTC 表示时间。 For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone.例如,一个时区的客户端浏览器getTime()将与任何其他时区的客户端浏览器相同。

In other words, it already does what you are asking.换句话说,它已经完成了您的要求。

You can also do the same thing without instantiating a Date object:你也可以在实例化Date对象的情况下做同样的事情:

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. Date.now()方法返回自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM