创建javascript日期UTC [英] create javascript date UTC

查看:160
本文介绍了创建javascript日期UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在控制台中输入以下代码:

Let's say I type the following code in the console:

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()我希望它说明2012年11月5日00:00:00 GMT。

The result is that the date is actually set to the local time. How do I create a Date that's set to UTC? If I do TheDate.toUTCString() I want it to say 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

接受与构造函数的最长形式相同的参数,并返回自1月以来Date对象中的毫秒数1,1970,00:00:00,世界时。

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日期UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆