简体   繁体   中英

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().toDate() returns my local time. Any help would be appreciated.

You can use 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

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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