简体   繁体   English

Moment.js获取UTC时间作为日期对象而不是字符串

[英]Moment.js to get UTC time as date object instead of string

I want to get the UTC time using moment.utc() as a date object instead of epoch or string ( moment.utc().format() or using .toISOString() ). 我想使用moment.utc()作为日期对象而不是纪元或字符串( moment.utc().format()或使用.toISOString() )获取UTC时间。 moment.utc().toDate() returns my local time. moment.utc().toDate()返回我的本地时间。 Any help would be appreciated. 任何帮助,将不胜感激。

You can use moment().toDate() . 您可以使用moment().toDate() If you coerce the date to a string (eg by sending it to the console, alert, etc.), the built-in (implementation dependent) toString method will typically use the host timezone settings to generate a string, eg 如果将日期强制为字符串(例如,通过将其发送到控制台,警报等),则内置(取决于实现)的toString方法通常将使用主机时区设置来生成字符串,例如

 var m = moment().toDate(); // Equivalent to new Date() console.log(m + '') // coerce to string uses built-in toString console.log(m.toISOString()) // ISO 8601 string offset +0000 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> 

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

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