假设我在控制台中键入以下代码:
var TheDate = new Date(2012, 10, 5);
TheDate.toUTCString();
"Sun, 04 Nov 2012 23:00:00 GMT" (I'm +1 hour ahead of GMT)
结果是日期实际上设置为当地时间.如何创建设置为UTC的日期?如果我做TheDate.toUTCString()我想要它说05年11月05日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.