假设我在控制台中键入以下代码:
var TheDate = new Date(2012, 10, 5);
TheDate.toUTCString();
"Sun, 04 Nov 2012 23:00:00 GMT" (I'm +1 hour ahead of GMT)
结果是日期实际上设置为本地时间。如何创 build 置为 UTC 的日期?如果我执行 TheDate.toUTCString()
,我希望它显示 05 Nov 2012 00:00:00 GMT。
谢谢。
最佳答案
使用 Date.UTC()
方法:
var TheDate = new Date( Date.UTC(2012, 10, 5) );
console.log( TheDate.toUTCString() );
返回
Mon, 05 Nov 2012 00:00:00 GMT
Date.UTC
Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.
关于javascript - 创建 javascript 日期 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13364036/