How to validate if a string is a valid date in js

2019-06-20 09:33发布

This question already has an answer here:

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?

1条回答
男人必须洒脱
2楼-- · 2019-06-20 10:14

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.

查看更多
登录 后发表回答