Javascript 日期对象总是有一天的差异吗?
在我的 Java Script 应用中,我以如下格式存储日期:
2011-09-24
现在,当我尝试使用上述值创建一个新的 Date 对象(这样我就可以以不同的格式检索日期)时,日期总是会差一天。见下文:
var date = new Date("2011-09-24");
console.log(date);
日志:
Fri Sep 23 2011 20:00:00 GMT-0400 (Eastern Daylight Time)
在使用 JS DATE 对象转换字符串时,会发生 一些疯狂 的事情,例如,考虑以下日期
Note: The following examples may or may not be ONE DAY OFF depending on YOUR timezone and current time.
new Date("2011-09-24"); // Year-Month-Day
// => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
However, if we rearrange the string format to Month-Day-Year ...
new Date("09-24-2011");
// => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
Another strange one
new Date("2011-09-24");
// => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF AS BEFORE.
new Date("2011/09/24"); // change from "-" to "/".
// => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
We could easily change hyphens in your date "2011-09-24" when making a new date
new Date("2011-09-24".replace(/-/g, '\/')); // => "2011/09/24".
// => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
What if we had a date string like "2011-09-24T00:00:00"
new Date("2011-09-24T00:00:00");
// => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
Now change hyphen to forward slash as before; what happens?
new Date("2011/09/24T00:00:00");
// => Invalid Date.
I typically have to manage the date format 2011-09-24T00:00:00 so this is what I do.
new Date("2011-09-24T00:00:00".replace(/-/g, '\/').replace(/T.+/, ''));
// => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
UPDATE
如果您向 Date 构造函数提供单独的参数,则可以获得其他有用的输出,如下所述
注意: 参数可以是 Number 或 String 类型。我将展示混合值的示例。
Get the first month and day of a given year
new Date(2011, 0); // Normal behavior as months in this case are zero based.
// => Sat Jan 01 2011 00:00:00 GMT-0700 (MST)
Get the last month and day of a year
new Date((2011 + 1), 0, 0); // The second zero roles back one day into the previous month's last day.
// => Sat Dec 31 2011 00:00:00 GMT-0700 (MST)
Example of Number, String arguments. Note the month is March because zero based months again.
new Date(2011, "02");
// => Tue Mar 01 2011 00:00:00 GMT-0700 (MST)
If we do the same thing but with a day of zero, we get something different.
new Date(2011, "02", 0); // Again the zero roles back from March to the last day of February.
// => Mon Feb 28 2011 00:00:00 GMT-0700 (MST)
Adding a day of zero to any year and month argument will get the last day of the previous month. If you continue with negative numbers you can continue rolling back another day
new Date(2011, "02", -1);
// => Sun Feb 27 2011 00:00:00 GMT-0700 (MST)
请注意,东部夏令时间为
-4 小时
,而您返回的日期的小时数为
20
。
20h + 4h = 24h
即 2011-09-24 午夜。由于您提供的是仅包含日期的字符串,而没有任何时区指示符,因此该日期以 UTC (GMT) 进行
解析
。如果您提供的是没有时区指示符的日期/时间字符串(
new Date("2011-09-24T00:00:00")
),则会以您的本地时区进行解析。 (从历史上看,那里存在不一致之处,尤其是因为规范更改了不止一次,但现代浏览器应该没问题;或者您始终可以包含时区指示器。)
您获得了正确的日期,只是您从未指定正确的时区。
如果您需要访问日期值,可以使用
getUTCDate()
或
任何其他
getUTC*()
函数
:
var d,
days;
d = new Date('2011-09-24');
days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
console.log(days[d.getUTCDay()]);
要规范化日期并消除不需要的偏移量(在此处测试: https://jsfiddle.net/7xp1xL5m/ ):
var doo = new Date("2011-09-24");
console.log( new Date( doo.getTime() + Math.abs(doo.getTimezoneOffset()*60000) ) );
// Output: Sat Sep 24 2011 00:00:00 GMT-0400 (Eastern Daylight Time)
这也完成了相同的操作并归功于@tpartee(在此处测试: https://jsfiddle.net/7xp1xL5m/1/ ):
var doo = new Date("2011-09-24");
console.log( new Date( doo.getTime() - doo.getTimezoneOffset() * -60000 ) );