首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将当前日期和时间表示为UTC (以天/分钟为单位)?

如何将当前日期和时间表示为UTC (以天/分钟为单位)?
EN

Stack Overflow用户
提问于 2019-11-20 21:07:32
回答 1查看 3.9K关注 0票数 1

我需要获取UTC格式的当前日期和时间,而不考虑当前时区。因此,如果我当前的本地时间是12:00,并且我在UTC+1:00,我希望获得12:00UTC。

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

EN

回答 1

Stack Overflow用户

发布于 2019-11-21 02:45:58

为此,您不需要库。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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 plugin,但其工作方式类似于Moment。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58955103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档