Search code examples
javascriptmomentjsdate-fns

Date-fns and Moment different results


I have this date string: 2020-01-21 and I get two different results between Date-Fns and Moment:

Date-fns: {format(new Date(startTime), 'MMM d, y')} => Jan 20, 2020
Moment:  {moment(startTime).format('MMM Do, YYYY')} => Jan 21st, 2020

Solution

  • date-fns made a change recently to how a string is parsed into a date. The current best practice would be to use parseISO as follows:

    parseISO('2020-01-21')
    

    The problem actually occurs before date-fns - in a browser console the following:

    new Date('2020-01-21')
    

    shows an incorrect date.