0

I have a date in this format "DD/MM/YYYY" when I try new Date("23/06/2019") I get the following error:

"Invalid Date"

how can I fix?

Francesco
  • 51
  • 2
  • 8
  • 1
    Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Heretic Monkey Dec 17 '19 at 23:06
  • See also [Convert dd-mm-yyyy string to date](https://stackoverflow.com/q/7151543/215552) – Heretic Monkey Dec 17 '19 at 23:08

2 Answers2

0

The string you pass in isn't something the Date 'constructor' knows how to deal with. Look at how to create a date object with javascript (Here's a SO post).

You can try this as a quick fix for now:

let yourDate = "23/06/2019".split('/');
new Date(yourDate[2], yourDate[1], yourDate[0]);

or

let yourDate = "23/06/2019".split('/').reverse().join('-');
new Date(yourDate);
Jojofoulk
  • 2,127
  • 1
  • 7
  • 25
  • correct your first solution, new Date(year,month,day), month are 0 to january, 1 for febrary... – Eliseo Dec 18 '19 at 07:51
0

I would recommend using the moment.js library for date parsing. You could easily parse your date string.

https://momentjs.com/docs/#/parsing/string-format/

moment(„23/09/2019“,“DD-MM-YYYY“)

Simon Echle
  • 286
  • 2
  • 9