有什么简单的方法来检查一个值是否为有效日期,允许任何已知的日期格式。

例如,我有值10-11-2009、10/11/2009、2009-11-10T07:00:00+0000,这些值都应该被识别为日期值,而值200、10、350不应该被识别为日期值。如果可能的话,最简单的检查方法是什么?因为时间戳也是允许的。


当前回答

我发现这个解决方案非常好:

const DateTime = require('luxon').DateTime;

isDateValid(stringDate) {
  let date = DateTime.fromFormat(stringDate, 'd-M-y');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'd,M,y');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'y-M-d');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'y,M,d');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'y.M.d');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'd.M.y');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'y/M/d');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  date = DateTime.fromFormat(stringDate, 'd/M/y');
  if (date.invalid === null) {
    return date.toJSDate();
  }
  return false;
}

isDateValid('30.12.86'); //true
isDateValid('30/12/86'); //true
isDateValid('86-12-40'); //false

而且您可以轻松地添加更多格式

其他回答

Date.parse()是否足够?

请参阅其相关MDN文档页面。

日期。如果字符串date有效,Parse返回时间戳。下面是一些用例:

// /!\ from now (2021) date interpretation changes a lot depending on the browser
Date.parse('01 Jan 1901 00:00:00 GMT') // -2177452800000
Date.parse('01/01/2012') // 1325372400000
Date.parse('153') // NaN (firefox) -57338928561000 (chrome)
Date.parse('string') // NaN
Date.parse(1) // NaN (firefox) 978303600000 (chrome)
Date.parse(1000) // -30610224000000 from 1000 it seems to be treated as year
Date.parse(1000, 12, 12) // -30610224000000 but days and month are not taken in account like in new Date(year, month,day...)
Date.parse(new Date(1970, 1, 0)) // 2588400000
// update with edge cases from comments
Date.parse('4.3') // NaN (firefox) 986248800000 (chrome)
Date.parse('2013-02-31') // NaN (firefox) 1362268800000 (chrome)
Date.parse("My Name 8") // NaN (firefox) 996616800000 (chrome)

new Date(Date) === '无效日期'只适用于Firefox和Chrome。IE8(我在我的机器上用于测试的那个)提供NaN。

正如已接受的答案所述,date .parse(date)也适用于数字。所以为了解决这个问题,你也可以检查它是不是一个数字(如果这是你想确认的事情)。

var parsedDate = Date.parse(date);

// You want to check again for !isNaN(parsedDate) here because Dates can be converted
// to numbers, but a failed Date parse will not.
if (isNaN(date) && !isNaN(parsedDate)) {
    /* do your work */
}

通过参考以上所有的评论,我已经找到了一个解决方案。

如果传递的日期是ISO格式或需要为其他格式操作,则此方法有效。

var isISO = "2018-08-01T18:30:00.000Z";

if (new Date(isISO) !== "Invalid Date" && !isNaN(new Date(isISO))) {
    if(isISO == new Date(isISO).toISOString()) {
        console.log("Valid date");
    } else {
        console.log("Invalid date");
    }
} else {
    console.log("Invalid date");
}

你可以在JSFiddle上玩。

这是一个极简的版本。

var isDate = function (date) {
    return!!(function(d){return(d!=='Invalid Date'&&!isNaN(d))})(new Date(date));
}

我觉得没有一个答案正确理解了OP的问题。这里的问题是JavaScript可以将任何数字解析为有效日期,因为date对象可以将'3000'这样的字符串解析为年份,并将返回一个有效的date实例:

new Date('3000')

> Wed Jan 01 3000 02:00:00 GMT+0200 (Eastern European Standard Time)

为了解决这个问题,我们可以在严格模式下通过传入第三个参数来使用Day.js库的解析方法。它被记录在他们的字符串+格式页面。为了使解析能够基于格式工作,我们还必须启用CustomParseFormat插件。我假设你可以在这里使用ESM导入,或者设置一个像Webpack这样的编译器

import dayjs from 'dayjs'
import formatParser from 'dayjs/plugin/customParseFormat'

dayjs.extend(formatParser)

dayjs('3000', 'YYYY-MM-DD', true).isValid()

> false