在我的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)

当前回答

试着在这个帖子中添加我的2分(详细说明@paul-wintz的答案)。

在我看来,当日期构造函数接收到匹配ISO 8601格式(日期部分)的第一部分的字符串时,它会在UTC时区与0时间进行精确的日期转换。当该日期转换为当地时间时,可能会发生日期移位 如果午夜UTC是本地时区的较早日期。

new Date('2020-05-07')
Wed May 06 2020 20:00:00 GMT-0400 (Eastern Daylight Time)

如果日期字符串是任何其他“松散”格式(使用“/”或日期/月不填充为零),则在本地时区创建日期,因此没有日期转移问题。

new Date('2020/05/07')
Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2020-5-07')
Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2020-5-7')
Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2020-05-7')
Thu May 07 2020 00:00:00 GMT-0400 (Eastern Daylight Time)

因此,如上所述,一个快速修复方法是将ISO格式的Date字符串中的“-”替换为“/”。

new Date('2020-05-07'.replace('-','/'))
Thu May 07 2020 00: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"

我遇到过这样的问题。但我的问题是off set,而从数据库获取日期。

这是在数据库中,它是UTC格式。

2019-03-29 19:00:00.0000000 +00:00

所以当我从数据库和检查日期,它是添加偏移量与它,并发送回javascript。

它正在添加+05:00,因为这是我的服务器时区。我的客户在不同的时区+07:00。

2019-03-28T19:00:00+05:00 //这是我在javascript中得到的。

所以这是我的解决方案,我所做的这个问题。

var dates = price.deliveryDate.split(/-|T|:/);
var expDate = new Date(dates[0], dates[1] - 1, dates[2], dates[3], dates[4]);
var expirationDate = new Date(expDate);

因此,当日期来自服务器,有服务器偏移量,所以我分割日期和删除服务器偏移量,然后转换为日期。它解决了我的问题。

这解决了我的问题(感谢@Sebastiao的回答)

var date = new Date();
//"Thu Jun 10 2021 18:46:00 GMT+0200 (Eastern European Standard Time)"

date.toString().split(/\+|-/)[0] ;  // .split(/\+|-/) is a regex for matching + or -
//"Thu Jun 10 2021 18:46:00 GMT"

var date_string_as_Y_M_D = (new Date(date)).toISOString().split('T')[0];
//2021-06-10

您正在使用ISO日期字符串格式,根据此页,将导致使用UTC时区构造日期:

注意:使用date构造函数(和 日期。解析,它们是等效的)是强烈不鼓励的,因为 浏览器差异和不一致。支持RFC 2822格式 字符串只是按照惯例。对ISO 8601格式的支持有所不同 只有日期的字符串(例如:"1970-01-01")被视为UTC,而不是 当地。

如果文本格式不同,例如“Jan 01 1970”,那么(至少在我的机器上)它使用您的本地时区。

这个通过我循环,zzzBov的答案是+1。这是一个完整的转换日期,为我工作使用UTC方法:

//myMeeting.MeetingDate = '2015-01-30T00:00:00'

var myDate = new Date(myMeeting.MeetingDate);
//convert to JavaScript date format
//returns date of 'Thu Jan 29 2015 19:00:00 GMT-0500 (Eastern Standard Time)' <-- One Day Off!

myDate = new Date(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate());
//returns date of 'Fri Jan 30 2015 00:00:00 GMT-0500 (Eastern Standard Time)' <-- Correct Date!