javascript 如何在NodeJS中获取UTC日期对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49646303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get UTC Date object in NodeJS?
提问by Nimish David Mathew
I want to get the Current Date object in UTC and then store it using Mongoose.
我想以 UTC 格式获取当前日期对象,然后使用 Mongoose 存储它。
I tried using new Date(), Date.now()etc. But all these returns local time. How do I get the UTC date object?
我尝试使用new Date(),Date.now()等等。但是所有这些回报本地时间。如何获取 UTC 日期对象?
I want the Date object, not string representation.
我想要 Date 对象,而不是字符串表示。
回答by Enric A.
Just use new Date(new Date().toUTCString())
只需使用 new Date(new Date().toUTCString())
After converting to UTC, we have to again convert it into Date object if we need a Date Object.
转换为 UTC 后,如果需要 Date 对象,则必须再次将其转换为 Date 对象。

