Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么JavaScript getDate()有时返回上一个日期?_Javascript_Datetime - Fatal编程技术网

为什么JavaScript getDate()有时返回上一个日期?

为什么JavaScript getDate()有时返回上一个日期?,javascript,datetime,Javascript,Datetime,这里有五个箱子。最后三个案例的结果令我惊讶。 // Firefox 40.0.3 // Eastern time zone (ET) var d; d = new Date("January 1, 2015"); alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString()); // Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT

这里有五个箱子。最后三个案例的结果令我惊讶。

// Firefox 40.0.3
// Eastern time zone (ET)

var d;
d = new Date("January 1, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT

d = new Date("January 2, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 2 UTC: Fri, 02 Jan 2015 05:00:00 GMT

d = new Date("January 1, 2015 00:00:00 GMT");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT

d = new Date("2015-01-01");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 11 Date: 31 UTC: Thu, 01 Jan 2015 00:00:00 GMT

d = new Date("2015-01-02");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Fri, 02 Jan 2015 00:00:00 GMT
UTC和当地时间之间一定存在差异。我看到了部分答案。函数getDate()根据本地时间返回指定的日期。

对于getDate(),00:00:00GMT似乎是本地时间的前一天,因此我得到的是前一天的日期,而不是我预期的日期。

也许我真正应该问的是,为什么日期构造函数有时将参数解释为本地时间,有时解释为UTC时间?这方面有什么规定

请参见

具体而言-“如果未指定时区,且字符串采用ES5认可的ISO格式,则假定UTC。GMT和UTC被视为等效。本地时区用于解释RFC2822第3.3节格式(或ES5中未被认可为ISO 8601的任何格式)中不包含时区信息的参数”

“给定日期字符串“2014年3月7日”,parse()假定为本地时区,但给定ISO格式(如“2014-03-07”)则假定为UTC时区。因此,使用这些字符串生成的日期对象将表示不同的时间点,除非系统设置为UTC本地时区。”

最后两个示例是“ES5认可的ISO格式”

日期(“2015年1月1日”)被解释为本地日期。
日期(“2015-01-01”)在ES5中被解释为UTC日期,在ES6中被解释为本地日期。
getDate()获取本地日期。
getUTCdate()获取UTC日期

当您混合并匹配本地和UTC时,您会得到令人惊讶的结果

谢谢,@preston-s。你的评论花了我很长时间才回答了我的问题。但是,有趣的是,日期(“2015-01-01”)的解释将从ES5中的UTC更改为ES6中的本地时间。有人知道不同的浏览器何时会切换到新的行为吗?我预计在那个时候会有一些事情发生

: '缺少时区偏移的值为“Z”。'

: '如果没有时区偏移,则日期时间将被解释为本地时间。'

鉴于所有这些信息,这里有两个解决方案可以跨ES5和ES6工作。其他几种解决方案也是可能的。
1) 使用本地日期输入和输出

var d;
d = new Date("January 1, 2015");
alert('Month: ' + d.getMonth() + ' Date: ' + d.getDate() + ' UTC: ' + d.toUTCString());
// Month: 0 Date: 1 UTC: Thu, 01 Jan 2015 05:00:00 GMT
2) 使用UTC日期输入和输出。请详细说明时区,以便在ES6中仍然有效

var d;
d = new Date("2015-01-01T00:00:00.000Z");
alert('UTC Month: ' + d.getUTCMonth() + ' UTC Date: ' + d.getUTCDate() + ' UTC: ' + d.toUTCString());
// UTC Month: 0 UTC Date: 1 UTC: Thu, 01 Jan 2015 00:00:00 GMT
如果您希望getDate()函数将日期返回为01而不是1,下面是它的代码。。。。 假设今天的日期是2018年11月1日

var today = new Date();
today = today.getFullYear()+ "-" + (today.getMonth() + 1) + "-" + today.getDate();      
console.log(today);       //Output: 2018-11-1


today = today.getFullYear()+ "-" + (today.getMonth() + 1) + "-" + ((today.getDate() < 10 ? '0' : '') + today.getDate());
console.log(today);        //Output: 2018-11-01
var today=新日期();
today=today.getFullYear()+“-”+(today.getMonth()+1)+“-”+today.getDate();
console.log(今天)//产出:2018-11-1
today=today.getFullYear()+“-”+(today.getMonth()+1)+“-”+((today.getDate()<10?'0':“”)+today.getDate());
console.log(今天)//产出:2018-11-01

当您只查看UTC字符串时很难看到…查看iso字符串…将显示您的时区偏移我认为您的答案是毫无疑问的主题,问题不是询问
getDate()
方法或返回日期格式正确的问题,包括日期是一位数字。