如何验证字符串是否是js中的有效日期 [英] How to validate if a string is a valid date in js
问题描述
我有字符串字段,用于获取不同的值。收到的一些价值是日期。现在我需要检查收到的价值是否是日期?收到的日期可以再次采用不同的格式。
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.
我试过 Date.parse()
,如果是格式化的话是 dd-mm-yyyy hh:mm
,但我收到了一些日期( 26/05/2015 06:20:57 +00 :00
)。
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?
推荐答案
如果 Date.parse()
对您来说还不够 - 但可能就够了 - 请参阅以下文档:
If Date.parse()
is not enough for you - but it may be enough - see the documentation at:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
然后你可以尝试:
- Moment.js
这是一个解析,验证,操纵,并在JavaScript中显示日期,它具有比标准更丰富的API JavaScript日期处理函数。
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.
这篇关于如何验证字符串是否是js中的有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!