30

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

4
  • What are those functions (format() and toDate()) supposed to be, or really where do they come from? Those are not standard JavaScript. Commented Jan 30, 2019 at 14:45
  • Where's the toDate() coming from? Check the syntax. I thought it should be format(toDate('2019-02-11T14:00:00'), 'yyyy-mm-ddThh:mm:ss') Commented Jan 30, 2019 at 14:46
  • date-fns has a parse function but no toDate function Commented Jan 30, 2019 at 14:54
  • @phuzi The new 2.0 alpha version has a toDate() function. date-fns.org/v2.0.0-alpha.26/docs/toDate Commented Jan 30, 2019 at 14:55

5 Answers 5

39

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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...
I have to agree on you. But in comparison to moment.js, date-fns is fast and lighter.
17

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'); 

3 Comments

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 I updated my answer. It works now for 1.x and 2.x.
parse requires 3 necessary arguments now: date-fns.org/v2.26.0/docs/parse. parse( "22/11/2021", "dd/MM/yyyy", new Date() )
13

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()) } 

Comments

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') 

2 Comments

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. stackoverflow.com/questions/3085937/…
1990-04-20T17:47:38.000000Z this is a valid time string, here time zone data is included in it.
0

Version 3.6.0 of date-fns

In date-fns version 3.6.0, you can reliably format a date string using the toDate function.

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

Demo

console.log(dateFns.format(dateFns.toDate('2019-02-11T14:00:00'), 'MM/dd/yyyy'))
<script src="https://cdn.jsdelivr.net/npm/[email protected]/cdn.min.js"></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.