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?
Store the date object in a variable. If the date is valid then the getTime() will always be equal to itself. If the date is Invalid then the getTime() will return NaN which is not equal to itself. The isValid() function is used to check the getTime() method is equal to itself or not.
parse() will parse numbers: for example Date. parse(1) will return a date. So here we check if s is not a number the, if it is a date.
Given date in format date, month and year in integer. The task is to find whether the date is possible on not. Valid date should range from 1/1/1800 – 31/12/9999 the dates beyond these are invalid. These dates would not only contains range of year but also all the constraints related to a calendar date.
One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With