14

I have string field which is used to get different values. Some of the values received are dates. Now I need to check if value received is date or not? The date received can be in different formats again.

I tried Date.parse(), it works if format is dd-mm-yyyy hh:mm, but I have some dates received in like (26/05/2015 06:20:57 +00:00).

How do I compare if string is valid date or not?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
DS2020
  • 279
  • 1
  • 4
  • 20
  • Try looking at moment.js (http://momentjs.com/docs/) its a pretty strong date library. You would be able to try to create a date with any value then check to see if it is valid – Seth McClaine Jun 15 '15 at 13:33
  • 1
    This may be a duplicate, but the linked dupe has mostly poor answers that simply rely on *Date.parse*, which is very unreliable for random formats. – RobG Jun 15 '15 at 14:20
  • 2
    It is fundamental to correctly parsing a date string that you can tell the parser what format it is in (or supply a format you **know** it will parse correctly). Otherwise, you can just try a sequence of different formats and stop when you get one that "works". Where the year is first, you can be reasonably certain of y-m-d sequence, but where the year is last, the prevalence of the illogical m/d/y sequence means you can't reliably tell if 04/05/2010 is 4 May or April 5. – RobG Jun 15 '15 at 14:25
  • @RobG Good point. I added some fresh info [**in this answer**](https://stackoverflow.com/questions/7445328/check-if-a-string-is-a-date-value/30870755#30870755) to the linked dupe. – rsp Jun 16 '15 at 14:42
  • I'm surprised that nobody mentioned the "luxon" package from the same moment.js team. https://moment.github.io/luxon This is a lifesaver. I have worked on many super complex projects where the validity of date, durations, and intervals are important and luxon seems to be the best in the market. – Rameez Rami Feb 28 '23 at 19:04

1 Answers1

13

If Date.parse() is not enough for you - but it may be enough - see the documentation at:

then you may try:

It is a library to parse, validate, manipulate, and display dates in JavaScript, that has a much richer API than the standard JavaScript date handling functions.

See also this answer for more libraries and links to tutorials.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • 1
    thanks. use of moment.js helps. I can parse string to match any format I want. – DS2020 Jun 16 '15 at 13:45
  • 3
    using moment.js just for validating a string does not look correct. Is there any pure JavaScript solution available ? – Kira Jan 18 '17 at 09:37
  • 2
    `Date.parse()` should not be used, since it will accept any string with a number in it as a valid date - check comments on [https://stackoverflow.com/a/7445368/1876359](https://stackoverflow.com/a/7445368/1876359). – ocramot Nov 14 '18 at 13:08
  • @ocramot Yes, you're right, see also [my other answer on that](https://stackoverflow.com/questions/7445328/check-if-a-string-is-a-date-value/30870755#30870755) where I explain it in more detail. – rsp Nov 15 '18 at 16:04