我想知道JS中有效日期对象和无效日期对象之间的区别,但无法弄清楚:

var d = new Date("foo");
console.log(d.toString()); // shows 'Invalid Date'
console.log(typeof d); // shows 'object'
console.log(d instanceof Date); // shows 'true'

编写isValidDate函数有什么想法吗?

Ash建议使用Date.parse来解析日期字符串,这提供了一种权威的方法来检查日期字符串是否有效。如果可能的话,我更希望我的API接受Date实例,并能够检查/断言它是否有效。Borgar的解决方案做到了这一点,但我需要在浏览器上进行测试。我还想知道是否有更优雅的方式。Ash让我考虑不让我的API接受Date实例,这是最容易验证的。Borgar建议测试Date实例,然后测试Date的时间值。如果日期无效,则时间值为NaN。我用ECMA-262检查了一下,这种行为符合标准,这正是我所要的。


当前回答

我很少推荐没有的图书馆。但考虑到目前为止的大量答案,似乎值得指出的是,流行的库“date fns”有一个函数isValid。以下文件摘自其网站:

isValid argument Before v2.0.0 v2.0.0 onward
new Date() true true
new Date('2016-01-01') true true
new Date('') false false
new Date(1488370835081) true true
new Date(NaN) false false
'2016-01-01' TypeError false
'' TypeError false
1488370835081 TypeError true
NaN TypeError false

其他回答

我的解决方案是简单地检查您是否获得了有效的日期对象:

实施

Date.prototype.isValid = function () {
    // An invalid date object returns NaN for getTime() and NaN is the only
    // object not strictly equal to itself.
    return this.getTime() === this.getTime();
};  

用法

var d = new Date("lol");

console.log(d.isValid()); // false

d = new Date("2012/09/11");

console.log(d.isValid()); // true

我认为这是一个漫长的过程。我们可以简化如下:

 function isValidDate(dateString) {
        debugger;
        var dateStringSplit;
        var formatDate;

        if (dateString.length >= 8 && dateString.length<=10) {
            try {
                dateStringSplit = dateString.split('/');
                var date = new Date();
                date.setYear(parseInt(dateStringSplit[2]), 10);
                date.setMonth(parseInt(dateStringSplit[0], 10) - 1);
                date.setDate(parseInt(dateStringSplit[1], 10));

                if (date.getYear() == parseInt(dateStringSplit[2],10) && date.getMonth()+1 == parseInt(dateStringSplit[0],10) && date.getDate() == parseInt(dateStringSplit[1],10)) {
                    return true;
                }
                else {
                    return false;
                }

            } catch (e) {
                return false;
            }
        }
        return false;
    }

纯JavaScript解决方案:

const date = new Date(year, (+month-1), day);
const isValidDate = (Boolean(+date) && date.getDate() == day);

也适用于闰年!

贷方至https://medium.com/@esganzerla/simple-date-validation-with-javascript-caea0f71883c

简单优雅的解决方案:

const date = new Date(`${year}-${month}-${day} 00:00`)
const isValidDate = (Boolean(+date) && date.getDate() == day)

来源:

[1] https://medium.com/@esganzerla/simple-date-validation-with-javascript-caea0f71883c

[2] JavaScript中new date()中显示的日期不正确

这里只有少数人(@Zen、@Dex、@wanglab……)对javascript容忍在2月、4月、6月等月份溢出日数。。。

如果指定要处理的格式(即yyyy-MM-dd),则在解决方案中根本不必使用javascript对象Date。

函数leapYear(年){return((年%4==0)&&(年%100!=0))||(年%400==0);}函数validateStr(dateStr){if(/^[0-9][0-9][0-9][0-9][0-9]-[0-9][9-9]-[0-9][0-9][0-9]$/.test(dateStr)==false){return false;}var m=parseInt(dateStr.substr(5,2));var d=parseInt(dateStr.substr(8,2));var y=parseInt(dateStr.substr(0,4));//您可以为大于5000:-D的值添加年度检查如果(m>12||d>31){return false;}否则,如果(m==2&&d>28){如果(d==29){if(!leapYear(y)){return false;}}其他{return false;}}否则如果(d>30&&(m==4||m==6||m==9||m===11)){return false;}返回true;}console.log(“2020-02-29:”+validateDateStr(“2020:02-29”));//真的console.log(“2020-02-30:”+validateDateStr(“2020:02-30”));//假的console.log(“2022-02-29:”+validateDateStr(“20202-02-29”));//假的console.log(“2021-02-28:”+validateDateStr(“2011-02-28”));//真的console.log(“2020-03-31:”+validateDateStr(“2020:03-31”));//真的console.log(“2020-04-30:”+validateDateStr(“2020:04-30”));//真的console.log(“2020-04-31:”+validateDateStr(“2020:04-31”));//假的console.log(“2020-07-31:”+validateDateStr(“2020:07-31”));//真的console.log(“2020-07-32:”+validateDateStr(“2020:07-32”));//假的控制台日志(“2020-08-31:”+validateDateStr(“2020:08-31”));//真的console.log(“2020-12-03:”+validateDateStr(“2020-102-03”));//真的console.log(“2020-13-03:”+validateDateStr(“2020-103-03”));//假的console.log(“0020-12-03:”+validateDateStr(“0020-102-03”));//真的//无效正则表达式console.log(“20-12-03:”+validateDateStr(“20-12-103”));//假的console.log(“2020-012-03:”+validateDateStr(“2020:012-03”));//假的console.log(“2020-12-003:”+validateDateStr(“2020-102-003”));//假的