0

I am using Javascript's inbuilt date function to validate if a date is valid. The date pattern is dynamic and it varies for a different system based on user preferred format.

The function new Date("02.03.2021") is working fine The function new Date("23.03.2021") is failing.

Notice that the date is > 12 (number of months)

PS: I cannot use external libraries

1 Answers1

2

The behaviour of Date.parse (and the Date constructor with a string argument, which calls it internally) is implementation defined unless the string is in the standard format specified here. This means that you cannot rely on the results of these functions to be consistent across browsers (or even different versions of the same browser).

You should write your own validation function that takes the user's preferred format into account.

JosephDaSilva
  • 1,107
  • 4
  • 5
  • Ah! Expected it to have a robust inbuilt function. Nevermind thank you :) – Vinay Prabhu Jun 20 '21 at 03:56
  • @VinayPrabhu—there is no such thing as a robust function that can parse a date in any format without being told the format, e.g. should "02.03.2021" be 2 March or February 3? How about 02-03-04? – RobG Jun 20 '21 at 05:35