Date-fns: DD/MM/YYYY format doesn't parse in 1.30.1

Created on 29 Jan 2019  ·  2Comments  ·  Source: date-fns/date-fns

parsing DD/MM/YYYY format returns Invalid Date
parse('23/03/2018')
Version: 1.30.1

🤹‍♂️ Support

Most helpful comment

@kossnocorp The worldwide most common order style is day, month, year accoring to Wikipedia. How can it not be supported?

In my opinion there should be an option to tell the parse function which format to use:
e.g. 01.02.2019 could be 1st of february or 2nd of janaury but only in the US.

Btw. in your example you wrote date, month, year but didn't you mean day, month, year?

All 2 comments

Unfortunately, it's not a valid date string. JavaScript (as well as date-fns) accepts ISO 8601 (and some other formats like RFC 5322 but it depends on the engine/browser implementation):

new Date('23/03/2018')
//=> Invalid Date

new Date('2018/03/23')
//=> Fri Mar 23 2018 00:00:00 GMT+0530 (India Standard Time)

In v2 we've introduced parse function that can help you digest the string, but the easiest solution is to use this function that I wrote just for you:

function parse(str) {
  const [date, month, year] = str.split('/').map(n => parseInt(n))
  return new Date(year, month - 1, date)
}

parse('23/03/2018')
//=> Fri Mar 23 2018 00:00:00 GMT+0530 (India Standard Time)

@kossnocorp The worldwide most common order style is day, month, year accoring to Wikipedia. How can it not be supported?

In my opinion there should be an option to tell the parse function which format to use:
e.g. 01.02.2019 could be 1st of february or 2nd of janaury but only in the US.

Btw. in your example you wrote date, month, year but didn't you mean day, month, year?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedrobrost picture pedrobrost  ·  3Comments

Vincz picture Vincz  ·  3Comments

mnquintana picture mnquintana  ·  3Comments

mrclayman picture mrclayman  ·  3Comments

melkishengue picture melkishengue  ·  3Comments