If I create a date using date-fns addYears()
I see the date as expected:
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:
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? 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. 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.
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.
let d = addYears(new Date("2015-01-01T00:00"), 1);
let f = format(d, "YYYY-MM-DD");
will output ---> 2016-01-01