Javascript 如何验证字符串是否是js中的有效日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30846357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate if a string is a valid date in js
提问by DS2020
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).
我试过了Date.parse(),如果格式是,它就可以工作dd-mm-yyyy hh:mm,但是我收到了一些类似 ( 26/05/2015 06:20:57 +00:00) 的日期。
How do I compare if string is valid date or not?
如何比较字符串是否为有效日期?
采纳答案by rsp
If Date.parse()is not enough for you - but it may be enough - see the documentation at:
如果Date.parse()对您来说还不够 - 但可能已经足够了 - 请参阅以下文档:
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.
它是一个在 JavaScript 中解析、验证、操作和显示日期的库,它具有比标准 JavaScript 日期处理函数更丰富的 API。
See also this answerfor more libraries and links to tutorials.
另请参阅此答案以获取更多库和教程链接。

