如何将当前日期和时间表示为UTC(以dayjs/minum表示)



无论当前时区如何,我都需要获得UTC的当前日期和时间。所以,如果我当前的当地时间是12:00,而我在UTC+1:00,我想得到12:00 UTC。

如何在moment.js或day.js中轻松实现这一点?

您不需要库。

new Date()                // gets a Date object representing "now"  (internally in UTC)
new Date().toISOString()  // returns "now", in UTC, formatted as an ISO 8601 string

如果你真的想使用Moment:

moment.utc()              // gets a Moment object representing "now", set to UTC mode
moment.utc().format()     // returns "now", in UTC, with the default ISO 8601 format
moment.utc().toISOString() // does the same thing
moment().toISOString()     // does the same thing

Day.js需要UTC插件,但其工作原理与Moment类似。

最新更新