[英]Date-fns date different after format()
If I create a date using date-fns addYears()
I see the date as expected: 如果我使用date-
addYears()
创建日期, addYears()
看到预期的日期:
let d = addYears(new Date('2015-01-01), 1) // 2016-01-01T00:00:00.000Z
However, when I use format()
to output as a string, I lose a day: 但是,当我使用
format()
作为字符串输出时,我损失了一天的时间:
let f = format(d, 'YYYY-MM-DD')
// expected output: 2016-01-01
// actual output: 2015-12-31
Is this a bug in format()
or the expected output? 这是
format()
的错误还是预期的输出? If so, is it possible to workaround this? 如果是这样,是否可以解决此问题?
I was just discussing this with someone. 我只是在和某人讨论。 If you append
T00:00
(explicitly setting the hours and minutes) to the date string it stores the correct date. 如果将
T00:00
(明确设置小时和分钟)添加到日期字符串,它将存储正确的日期。 If the hours and minutes are omitted the time zone offset, date-times are interpreted as user local time. 如果省略了小时和分钟,则时区偏移量,日期时间将解释为用户本地时间。 When you omit the time altogether, dates are interpreted as UTC.
当您完全省略时间时,日期将解释为UTC。
So foo = new Date('2015-01-01T00:00')
should store the correct date, then use the format.()
function as you are, it will output the correct date. 因此
foo = new Date('2015-01-01T00:00')
应该存储正确的日期,然后按原样使用format.()
函数,它将输出正确的日期。
let d = addYears(new Date("2015-01-01T00:00"), 1);
let f = format(d, "YYYY-MM-DD");
will output ---> 2016-01-01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.