在我的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)
当前回答
这解决了我的问题(感谢@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
其他回答
// 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"
试着在这个帖子中添加我的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)
使用moment可以在转换为isostring时保持偏移量
let date = moment("2022-03-15").toISOString();
// WRONG OUTPUT 2022-03-14T18:30:00.000Z
let date = moment("2022-03-15").toISOString(true);
// CORRECT OUTPUT 2022-03-15T00:00:00.000+05:30
如果你只是想确保日期的各个部分保持不变,即使我改变了时区,这个方法似乎也能奏效:
var doo = new Date("2011-09-24 00:00:00")
只要把0加进去。
在我的代码中,我这样做:
let dateForDisplayToUser =
new Date( `${YYYYMMDDdateStringSeparatedByHyphensFromAPI} 00:00:00` )
.toLocaleDateString(
'en-GB',
{ day: 'numeric', month: 'short', year: 'numeric' }
)
我在我的电脑上切换时区,日期保持不变,我从API得到的日期字符串yyyy-mm-dd。
但我是不是遗漏了什么/这是个坏主意?
*至少在chrome中。这在Safari中不起作用!撰写本文时
要规范化日期并消除不需要的偏移量(在这里测试: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 ) );