Javascript 在javascript中检查日期是否有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10589732/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:00:42  来源:igfitidea点击:

Checking if a date is valid in javascript

javascript

提问by Guilherme Costa

Possible Duplicate:
Detecting an “invalid date” Date instance in JavaScript

可能的重复:
在 JavaScript 中检测“无效日期”日期实例

Is there is a function IsDate()in javascript?

IsDate()javascript中有函数吗?

回答by Gabriel Jürgens

Try this:

尝试这个:

var date = new Date();
console.log(date instanceof Date && !isNaN(date.valueOf()));

This should return true.

这应该返回true

UDATED: Added isNaNcheck to handle the case commented by Julian H. Lam

最新:添加isNaN检查以处理Julian H. Lam评论的案例