在我的Java脚本应用程序中,我以这样的格式存储日期:
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)
在我的Java脚本应用程序中,我以这样的格式存储日期:
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)
当前回答
// When the time zone offset is absent, date-only formats such as '2011-09-24' // are interpreted as UTC time, however the date object will display the date // relative to your machine's local time zone, thus producing a one-day-off output. const date = new Date('2011-09-24'); console.log(date); // Fri Sep 23 2011 17:00:00 GMT-0700 (PDT) console.log(date.toLocaleDateString('en-US')); // "9/23/2011" // To ensure the date object displays consistently with your input, simply set // the timeZone parameter to 'UTC' in your options argument. console.log(date.toLocaleDateString('en-US', { timeZone: 'UTC' })); // "9/24/2011"
其他回答
你的问题是时区。注意GMT-0400部分,也就是你比GMT晚4个小时。如果在显示的日期/时间上加上4个小时,就会得到2011/09/24的零点。使用toUTCString()方法来获取GMT字符串:
var doo = new Date("2011-09-24");
console.log(doo.toUTCString());
您正在使用ISO日期字符串格式,根据此页,将导致使用UTC时区构造日期:
注意:使用date构造函数(和 日期。解析,它们是等效的)是强烈不鼓励的,因为 浏览器差异和不一致。支持RFC 2822格式 字符串只是按照惯例。对ISO 8601格式的支持有所不同 只有日期的字符串(例如:"1970-01-01")被视为UTC,而不是 当地。
如果文本格式不同,例如“Jan 01 1970”,那么(至少在我的机器上)它使用您的本地时区。
要规范化日期并消除不需要的偏移量(在这里测试: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 ) );
遵循代码对我很有效。首先,我将日期和时间字符串转换为localeDateString,然后对返回的字符串应用split函数。
const dateString = "Thu Dec 29 2022 00:00:00 GMT+0500 (Pakistan Standard Time)";
const date = new Date(dateString).toLocaleDateString().split("/");
const year = new Date(dateString).getFullYear();
const month = new Date(dateString).getMonth();
console.log(new Date(`${date[2]}-${date[0]}-${date[1]}`));
// 2022-12-29T00:00:00.000Z
// Due to timezone issue, the date is one day off.
console.log(new Date("2011-09-24"));
// => 2011-09-24T00:00:00.000Z-CORRECT DATE.
console.log(new Date("2011/09/24"));
// => 2011-09-23T19:00:00.000Z -ONE DAY OFF AS BEFORE.
// When the time zone offset is absent, date-only formats such as '2011-09-24' // are interpreted as UTC time, however the date object will display the date // relative to your machine's local time zone, thus producing a one-day-off output. const date = new Date('2011-09-24'); console.log(date); // Fri Sep 23 2011 17:00:00 GMT-0700 (PDT) console.log(date.toLocaleDateString('en-US')); // "9/23/2011" // To ensure the date object displays consistently with your input, simply set // the timeZone parameter to 'UTC' in your options argument. console.log(date.toLocaleDateString('en-US', { timeZone: 'UTC' })); // "9/24/2011"