Skip to content

incorrect year when formatting date with timezone #2133

@hamiltonchua

Description

@hamiltonchua

Using datefns v 2.16.1

Given a date string

2020-12-27T08:00:00.000Z

The result of formatting the date with the specified format

yyyy-MM-dd : 2020-12-27
Y-MM-dd : 2021-12-27

Y is one year ahead.

Note that if the date string is

2020-12-26T08:00:00.000Z

where the day is "26" instead of "27" the value of Y is 2020 as expected

Some sample code below to replicate:

const {
    format
} = require('date-fns');

let dString = '2020-12-27T08:00:00.000Z';

console.log('date string :', dString);

let dateToPublish;

dateToPublish = format(new Date(dString), 'yyyy-MM-dd');
console.log('format: yyyy-MM-dd :', dateToPublish);

dateToPublish = format(new Date(dString), 'Y-MM-dd');
console.log('format: Y-MM-dd :', dateToPublish);

The output :

date string : 2020-12-27T08:00:00.000Z
format: yyyy-MM-dd : 2020-12-27
format: Y-MM-dd : 2021-12-27

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions