-1

How can I format date from DD.MM.YY format to any other format (preferably DD/MM/YYYY) with full 4 digits year value that I can input to new Date() by using javascript?

I find similar issue but in PHP code, is there an library that can convert that in javascript?

I'm getting Invalid Date error when inputting that date format to new Date()

Hadriaki
  • 11
  • 4
  • 1
    When you have two digits for the year, what's your cutoff for when is in the 1900's and when is in the 2000's? – WilliamNHarvey Jul 29 '19 at 22:11
  • DD.MM.YY is not a format supported by ECMA-262, so parsing is implementation dependent. An invalid date is a conforming result. Framing of 2 digit years to the correct century usually requires different business rules depending on the application. – RobG Jul 30 '19 at 01:09

1 Answers1

-1

Vanilla JavaScript can be used to convert the date string into a Date object by calling split(",") on the string, parseInt() on each returned array item, and then adding 1900 or 2000 to the year component based on your own needs. Date() takes the year, month, and day as arguments.
Once you have a JavaScript date object, a method like this using padStart() can output the date into the DD/MM/YYYY format.
See this example codepen: https://codepen.io/volleio/pen/BXWKyp

Lucas
  • 129
  • 10