有什么简单的方法来检查一个值是否为有效日期,允许任何已知的日期格式。
例如,我有值10-11-2009、10/11/2009、2009-11-10T07:00:00+0000,这些值都应该被识别为日期值,而值200、10、350不应该被识别为日期值。如果可能的话,最简单的检查方法是什么?因为时间戳也是允许的。
有什么简单的方法来检查一个值是否为有效日期,允许任何已知的日期格式。
例如,我有值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
而且您可以轻松地添加更多格式
其他回答
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 */
}
下面是可以用来验证输入是否为可以转换为日期对象的数字或字符串的方法。
它涵盖以下情况:
捕获任何导致“无效日期”日期构造函数结果的输入; 捕捉从技术角度来看日期是“有效的”,但从业务逻辑角度来看它是无效的情况,例如
new Date(null).getTime(): 0 new Date(true).getTime(): 1 new Date(-3.14).getTime(): -3 new Date(["1", "2"]).toDateString(): Tue Jan 02 2001 new Date([1,2]).toDateString(): Tue Jan 02 2001
function checkDateInputValidity(input, lowerLimit, upperLimit) {
// make sure the input is a number or string to avoid false positive correct dates:
if (...) {
return false
}
// create the date object:
const date = new Date(input)
// check if the Date constructor failed:
if (date.toDateString() === 'Invalid Date') {
return false
}
// check if the Date constructor succeeded, but the result is out of range:
if (date < new Date(lowerLimit) || date > new Date(upperLimit)) {
return false
}
return true
}
// const low = '2021-12-31T23:59:59'
// const high = '2025-01-01T00:00:00'
好吧,这是一个老问题,但我在检查这里的解时找到了另一个解。For me工作检查函数getTime()是否存在于日期对象:
const checkDate = new Date(dateString);
if (typeof checkDate.getTime !== 'function') {
return;
}
像这样的怎么样?它将测试它是一个Date对象还是一个日期字符串:
function isDate(value) {
var dateFormat;
if (toString.call(value) === '[object Date]') {
return true;
}
if (typeof value.replace === 'function') {
value.replace(/^\s+|\s+$/gm, '');
}
dateFormat = /(^\d{1,4}[\.|\\/|-]\d{1,2}[\.|\\/|-]\d{1,4})(\s*(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d\s*[ap]m)?$/;
return dateFormat.test(value);
}
我应该提到的是,这并不测试ISO格式的字符串,但只要再对RegExp做一些工作,您就可以了。
通过参考以上所有的评论,我已经找到了一个解决方案。
如果传递的日期是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上玩。