23

I'm trying out date-fns v2.

I want to format a date in a string by using the toDate and format functions:

import { format, toDate } from 'date-fns'
format(toDate('2019-02-11T14:00:00'), 'MM/dd/yyyy')

But get the following error:

RangeError: Invalid time value

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

4 Answers4

34

It seems that you are using Version 2.0 of date-fns, which is still in alpha (development) status currently.

What functions are available and how they work still seems to change frequently. For example, while in version v2.0.0-alpha.26 toDate() can handle string parameters, it cannot do that any longer in version v2.0.0-alpha.27. There is a new parseISO() function instead.

This should work now:

format(parseISO('2019-02-11T14:00:00'), 'MM/dd/yyyy')

However, while version 2 is still in beta, I would suggest using the stable version 1.x for now.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • 1
    In fact, I would suggest not using the library at all. Introducing all those breaking changes going from version 1 to version 2 and again completely changing the interface multiple times while developing version 2 means they will probably introduce further breaking changes in the future... – NineBerry Jan 30 '19 at 15:26
  • 2
    I have to agree on you. But in comparison to moment.js, date-fns is fast and lighter. – Zubli Quzaini Aug 21 '20 at 17:34
16

date-fns 2.0.0-alpha.27 (demo: https://stackblitz.com/edit/js-tztuz6)

Use parseISO:

import { format, parseISO } from 'date-fns'
const formattedDate = format(parseISO('2019-02-11T14:00:00'), 'MM/dd/yyyy');

date-fns v1.30.1 (demo: https://stackblitz.com/edit/js-mt3y6p)

Use parse:

import { format, parse } from 'date-fns'
const formattedDate = format(parse('2019-02-11T14:00:00'), 'MM/DD/YYYY');
Dawid Karabin
  • 5,113
  • 1
  • 23
  • 31
  • 1
    The parse() in 2.x works different than in 1.x. In 2.x it now always requires a second parameter for specifying to format to understand. – NineBerry Jan 30 '19 at 15:19
  • @NineBerry I updated my answer. It works now for 1.x and 2.x. – Dawid Karabin Jan 30 '19 at 15:24
  • 2
    parse requires 3 necessary arguments now: https://date-fns.org/v2.26.0/docs/parse. ````parse( "22/11/2021", "dd/MM/yyyy", new Date() )```` – Aman Godara Nov 22 '21 at 08:33
10

Btw, one more case to validate:

import { isValid, parseISO, parse } from 'date-fns'

// Date valid
if (isValid(parseISO('2019-11-27 09:45:00'))) {
 let dt = parse('2019-11-27 09:45:00', 'yyyy-MM-dd HH:mm:ss', new Date())
}
bravohex
  • 966
  • 13
  • 21
0

The method toDate is not able to read that format.

Try to convert to Date lke this:

var date = new Date('2019-02-11T14:00:00')
IrishJS
  • 27
  • 5
  • 1
    Downvoted because attempting to do this is what made me look into date-fns in the first place - this was exactly the format that Safari threw an "Invalid date" error on. https://stackoverflow.com/questions/3085937/safari-js-cannot-parse-yyyy-mm-dd-date-format – Jonas Rosenqvist May 06 '21 at 12:19
  • `1990-04-20T17:47:38.000000Z` this is a valid time string, here time zone data is included in it. – Md. A. Apu Sep 28 '21 at 10:35