简体   繁体   English

检查字符串是否为日期值

[英]Check if a string is a date value

What is an easy way to check if a value is a valid date, any known date format allowed.检查一个值是否为有效日期的简单方法是什么,允许任何已知的日期格式。

For example I have the values 10-11-2009 , 10/11/2009 , 2009-11-10T07:00:00+0000 which should all be recognized as date values, and the values 200 , 10 , 350 , which should not be recognized as a date value.例如,我的值10-11-200910/11/20092009-11-10T07:00:00+0000应该都被识别为日期值,而值20010350不应该被识别为日期值。 What is the simplest way to check this, if this is even possible?如果可能的话,最简单的检查方法是什么? Because timestamps would also be allowed.因为时间戳也将被允许。

2015 Update 2015年更新

It is an old question but other new questions like:这是一个老问题,但还有其他新问题,例如:

get closed as duplicates of this one, so I think it's important to add some fresh info here.作为这个副本的副本被关闭,所以我认为在这里添加一些新信息很重要。 I'm writing it because I got scared thinking that people actually copy and paste some of the code posted here and use it in production.我写它是因为我害怕人们实际上复制和粘贴这里发布的一些代码并在生产中使用它。

Most of the answers here either use some complex regular expressions that match only some very specific formats and actually do it incorrectly (like matching January 32nd while not matching actual ISO date as advertised - seedemo ) or they try to pass anything to the Date constructor and wish for the best.这里的大多数答案要么使用一些复杂的正则表达式,这些表达式只匹配一些非常特定的格式并且实际上做错了(比如匹配 1 月 32 日,而不匹配宣传的实际 ISO 日期 - 请参阅演示),或者他们尝试将任何内容传递给Date构造函数并希望一切顺利。

Using Moment使用时刻

As I explained in this answer there is currently a library available for that: Moment.js正如我在这个答案中所解释的,目前有一个可用的库: Moment.js

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。

It is 12kB minified/gzipped and works in Node.js and other places:它是 12kB 压缩/gzipped 并在 Node.js 和其他地方工作:

bower install moment --save # bower
npm install moment --save   # npm
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor

Using Moment you can be very specific about checking valid dates.使用 Moment,您可以非常具体地检查有效日期。 Sometimes it is very important to add some clues about the format that you expect.有时,添加一些有关您期望的格式的线索非常重要。 For example, a date such as 06/22/2015 looks like a valid date, unless you use a format DD/MM/YYYY in which case this date should be rejected as invalid.例如,诸如 06/22/2015 之类的日期看起来像是一个有效日期,除非您使用 DD/MM/YYYY 格式,在这种情况下,应将该日期视为无效日期而拒绝。 There are few ways how you can tell Moment what format you expect, for example:有几种方法可以告诉 Moment 您期望的格式,例如:

moment("06/22/2015", "MM/DD/YYYY", true).isValid(); // true
moment("06/22/2015", "DD/MM/YYYY", true).isValid(); // false

The true argument is there so the Moment won't try to parse the input if it doesn't exactly conform to one of the formats provided (it should be a default behavior in my opinion). true论点在那里,所以如果输入不完全符合所提供的格式之一,则 Moment 不会尝试解析输入(在我看来,这应该是默认行为)。

You can use an internally provided format:您可以使用内部提供的格式:

moment("2015-06-22T13:17:21+0000", moment.ISO_8601, true).isValid(); // true

And you can use multiple formats as an array:您可以使用多种格式作为数组:

var formats = [
    moment.ISO_8601,
    "MM/DD/YYYY  :)  HH*mm*ss"
];
moment("2015-06-22T13:17:21+0000", formats, true).isValid(); // true
moment("06/22/2015  :)  13*17*21", formats, true).isValid(); // true
moment("06/22/2015  :(  13*17*21", formats, true).isValid(); // false

See:DEMO .见:演示

Other libraries其他图书馆

If you don't want to use Moment.js, there are also other libraries:如果您不想使用 Moment.js,还有其他库:

2016 Update 2016年更新

I created the immoment module that is like (a subset of) Moment but without surprises caused by mutation of existing objects (see the docs for more info).我创建了immoment模块,它类似于 Moment(的一个子集),但没有由现有对象的突变引起的意外(有关更多信息,请参阅文档)。

2018 Update 2018 更新

Today I recommend using Luxon for date/time manipulation instead of Moment, which (unlike Moment) makes all object immutable so there are no nasty surprises related to implicit mutation of dates.今天,我建议使用Luxon进行日期/时间操作,而不是 Moment,后者(与 Moment 不同)使所有对象不可变,因此不会出现与日期隐式突变相关的令人讨厌的意外。

More info更多信息

See also:也可以看看:

A series of articles by Rob Gravelle on JavaScript date parsing libraries: Rob Gravelle 关于 JavaScript 日期解析库的一系列文章:

Bottom line底线

Of course anyone can try to reinvent the wheel, write a regular expression (but please actually read ISO 8601 and RFC 3339 before you do it) or call buit-in constructors with random data to parse error messages like 'Invalid Date' (Are you sure this message is exactly the same on all platforms? In all locales? In the future?) or you can use a tested solution and use your time to improve it, not reinvent it.当然,任何人都可以尝试重新发明轮子,编写正则表达式(但在开始之前实际阅读 ISO 8601 和 RFC 3339)或使用随机数据调用内置构造函数来解析诸如'Invalid Date''Invalid Date'错误消息(是吗?确定此消息在所有平台上都完全相同?在所有语言环境中?将来?)或者您可以使用经过测试的解决方案并花时间改进它,而不是重新发明它。 All of the libraries listed here are open source, free software.这里列出的所有库都是开源的免费软件。

This is how I solved this problem in an app I'm working on right now:这就是我在我现在正在开发的应用程序中解决这个问题的方法:

updated based on feedback from krillgar:根据 krillgar 的反馈更新:

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

Would Date.parse() suffice? Date.parse()就足够了吗?

See its relative MDN Documentation page .请参阅其相关的MDN 文档页面

Date.parse returns a timestamp if string date is valid.如果字符串日期有效,则Date.parse返回时间戳。 Here are some use cases:以下是一些用例:

// /!\ 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) === 'Invalid Date' only works in Firefox and Chrome. new Date(date) === 'Invalid Date'仅适用于 Firefox 和 Chrome。 IE8 (the one I have on my machine for testing purposes) gives NaN. IE8(我在我的机器上用于测试目的的那个)给出了 NaN。

As was stated to the accepted answer, Date.parse(date) will also work for numbers.正如接受的答案所述, Date.parse(date)也适用于数字。 So to get around that, you could also check that it is not a number (if that's something you want to confirm).因此,为了解决这个问题,您还可以检查它是否不是数字(如果您想确认的话)。

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 */
}
function isDate(dateStr) {
  return !isNaN(new Date(dateStr).getDate());
}
  • This will work on any browser since it does not rely on "Invalid Date" check.这将适用于任何浏览器,因为它不依赖于“无效日期”检查。
  • This will work with legacy code before ES6.这将适用于 ES6 之前的遗留代码。
  • This will work without any library.这将在没有任何库的情况下工作。
  • This will work regardless of any date format.无论日期格式如何,这都将起作用。
  • This does not rely on Date.parse which fails the purpose when values like "Spiderman 22" are in date string.这不依赖于 Date.parse 当像“蜘蛛侠 22”这样的值在日期字符串中时,它会失败。
  • This does not ask us to write any RegEx.这并不要求我们编写任何 RegEx。

How about something like this?这样的事情怎么样? It will test if it is a Date object or a date string:它将测试它是 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);
}

I should mention that this doesn't test for ISO formatted strings but with a little more work to the RegExp you should be good.我应该提一下,这不会测试 ISO 格式的字符串,但对 RegExp 做更多的工作,你应该会很好。

None of the answers here address checking whether the date is invalid such as February 31. This function addresses that by checking if the returned month is equivalent to the original month and making sure a valid year was presented.这里的答案都没有解决检查日期是否无效的问题,例如 2 月 31 日。此函数通过检查返回的月份是否等于原始月份并确保提供了有效年份来解决该问题。

//expected input dd/mm/yyyy or dd.mm.yyyy or dd-mm-yyyy
function isValidDate(s) {
  var separators = ['\\.', '\\-', '\\/'];
  var bits = s.split(new RegExp(separators.join('|'), 'g'));
  var d = new Date(bits[2], bits[1] - 1, bits[0]);
  return d.getFullYear() == bits[2] && d.getMonth() + 1 == bits[1];
} 

Use Regular expression to validate it.使用正则表达式来验证它。

isDate('2018-08-01T18:30:00.000Z');

isDate(_date){
        const _regExp  = new RegExp('^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$');
        return _regExp.test(_date);
    }

By referring to all of the above comments, I have come to a solution.通过参考以上所有评论,我得出了一个解决方案。

This works if the Date passed is in ISO format or need to manipulate for other formats.如果传递的Date为 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");
}

You can play here on JSFiddle.你可以在这里玩 JSFiddle。

Here is an improved function that uses only Date.parse() :这是一个仅使用Date.parse()的改进函数:

function isDate(s) {
    if(isNaN(s) && !isNaN(Date.parse(s)))
        return true;
    else return false;
}

Note: Date.parse() will parse numbers: for example Date.parse(1) will return a date.注意: Date.parse() 将解析数字:例如Date.parse(1)将返回一个日期。 So here we check if s is not a number the, if it is a date.所以在这里我们检查s是否不是数字,如果它是日期。

I feel none of the answers correctly understood what the OP asked.我觉得没有一个答案正确理解 OP 的要求。 The issue here is that JavaScript can parse any number as a valid date, since the Date object can parse a string like '3000' as the year and will return a valid Date instance:这里的问题是 JavaScript 可以将任何数字解析为有效日期,因为Date对象可以将像'3000'这样的字符串解析为年份,并将返回一个有效的 Date 实例:

new Date('3000')

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

To solve this, we can use the Day.js library's parsing method in strict mode by passing in a third argument.为了解决这个问题,我们可以通过传入第三个参数在严格模式下使用Day.js库的解析方法。 It's documented in their String + Format page.它记录在他们的String + Format页面中。 In order for parsing to work based on a format, we must also enable the CustomParseFormat plugin.为了基于格式进行解析,我们还必须启用CustomParseFormat插件。 I'm assuming you can use ESM imports here or have a compiler like Webpack set up我假设你可以在这里使用 ESM 导入或者设置一个像 Webpack 这样的编译器

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

dayjs.extend(formatParser)

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

> false

I would do this我会这样做

var myDateStr= new Date("2015/5/2");

if( ! isNaN ( myDateStr.getMonth() )) {
    console.log("Valid date");
}
else {
    console.log("Invalid date");
}

Play here在这里

I know it's an old question but I faced the same problem and saw that none of the answers worked properly - specifically weeding out numbers (1,200,345,etc..) from dates, which is the original question.我知道这是一个老问题,但我遇到了同样的问题,发现没有一个答案正常工作 - 特别是从日期中剔除数字(1,200,345 等),这是原始问题。 Here is a rather unorthodox method I could think of and it seems to work.这是我能想到的一种相当非正统的方法,它似乎有效。 Please point out if there are cases where it will fail.请指出是否有失败的情况。

if(sDate.toString() == parseInt(sDate).toString()) return false;

This is the line to weed out numbers.这是清除数字的行。 Thus, the entire function could look like:因此,整个函数可能如下所示:

 function isDate(sDate) { if(sDate.toString() == parseInt(sDate).toString()) return false; var tryDate = new Date(sDate); return (tryDate && tryDate.toString() != "NaN" && tryDate != "Invalid Date"); } console.log("100", isDate(100)); console.log("234", isDate("234")); console.log("hello", isDate("hello")); console.log("25 Feb 2018", isDate("25 Feb 2018")); console.log("2009-11-10T07:00:00+0000", isDate("2009-11-10T07:00:00+0000"));

Here's a minimalist version.这是一个极简版本。

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

After trying all the answers listed above I ended up with the following: 在尝试了上面列出的所有答案之后,我得出以下结论:

var checkDateValue = function(date) {
    return date && (!(new Date(date) == "Invalid Date") && !isNaN(new Date(date)));
};

Note that new Date(date) !== "Invalid Date" is always true. 请注意, new Date(date) !== "Invalid Date" 始终为 true。 Hope this helps. 希望这可以帮助。

This callable function works perfectly, returns true for valid date.这个可调用函数完美运行,对于有效日期返回 true。 Be sure to call using a date on ISO format (yyyy-mm-dd or yyyy/mm/dd):请务必使用 ISO 格式的日期(yyyy-mm-dd 或 yyyy/mm/dd)调用:

function validateDate(isoDate) {

    if (isNaN(Date.parse(isoDate))) {
        return false;
    } else {
        if (isoDate != (new Date(isoDate)).toISOString().substr(0,10)) {
            return false;
        }
    }
    return true;
}

is it fine to check for a Date related function is available for the object to find whether it is a Date object or not ?检查对象是否可以使用与日期相关的函数以查找它是否是日期对象是否可以?

like喜欢

var l = new Date();
var isDate = (l.getDate !== undefined) ? true; false;

Date can be validated using following answer 可以使用以下答案验证日期

//format the date values dd-mm-yyyy or dd/mm/yyyy into following variables year month and date
 var year=2019;
 var month=2;
 var date=31;
 var d = new Date(year, month - 1, date);
 if (d.getFullYear() != year
    || d.getMonth() != (month - 1)
    || d.getDate() != date) {
alert("invalid date");
return false;
}else{
alert("Valid date");
}

Ok, this is an old question, but I found another solution while checking the solutions here.好的,这是一个老问题,但我在此处检查解决方案时找到了另一个解决方案。 For me works to check if the function getTime() is present at the date object:对我来说,检查函数 getTime() 是否存在于日期对象中:

const checkDate = new Date(dateString);

if (typeof checkDate.getTime !== 'function') {
  return;
}

I created a moment.js-based solution for this to detect ISO 8601, RFC 2822, and local formats, without having to specify parsing string (which I always don't want to type "standard" format.)... 我为此创建了一个基于基于moment.js的解决方案,以检测ISO 8601,RFC 2822和本地格式,而无需指定解析字符串(我始终不希望输入“标准”格式。)...

https://github.com/patarapolw/valid-moment https://github.com/patarapolw/valid-moment

一个简短的方法:

if(!isNaN(Date.parse(date)))

i find this solution very good:我发现这个解决方案非常好:

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

and you can easily add more formats并且您可以轻松添加更多格式

I think the most straight forward solution would be我认为最直接的解决方案是

if (Date.parse(yourDate) > 0) ? true : false;

If it is not a valid date, it will be NaN, which is not greater than 0.如果不是有效日期,则为 NaN,大于 0。

I believe this is the simplest working answer for date that contains only numbers:我相信这是仅包含数字的日期最简单的工作答案:

var rst = Date.parse(sDate.replaceAll(" ",""));
if(rst===NaN) console.log("not a date");
else console.log("a great date")

By removing spaces you detect values like "hello 2" that are taken as a date.通过删除空格,您可以检测到像“hello 2”这样被当作日期的值。 For the dates that contain strings like day name or month name... I believe it is about string validation.对于包含日期名称或月份名称等字符串的日期......我相信这是关于字符串验证的。

This is how I end up doing it.这就是我最终这样做的方式。 This will not cover all formats.这不会涵盖所有格式。 You have to adjust accordingly.你必须相应地调整。 I have control on the format, so it works for me我可以控制格式,所以它对我有用

function isValidDate(s) {
            var dt = "";
            var bits = [];
            if (s && s.length >= 6) {
                if (s.indexOf("/") > -1) {
                    bits = s.split("/");
                }
                else if (s.indexOf("-") > -1) {
                    bits = s.split("-");
                }
                else if (s.indexOf(".") > -1) {
                    bits = s.split(".");
                }
                try {
                    dt = new Date(bits[2], bits[0] - 1, bits[1]);
                } catch (e) {
                    return false;
                }
                return (dt.getMonth() + 1) === parseInt(bits[0]);
            } else {
                return false;
            }
        }

Try this 试试这个

<input type="text" id="StartDay" value="2018/01/01" maxlength="10" />

$('#StartDay').change(function () {
    if ( ($('#StartDay').val().length == 10 && new Date($('#StartDay').val()) >= new Date("2018/01/01") &&
        (new Date($('#StartDay').val()) !== "Invalid Date") && !isNaN(new Date($('#StartDay').val()))) == false) {
     .....
    }
})

Not successfully related to the question but... 没有成功地与问题相关但...

Check ISO data (ES6 functional way) 检查ISO数据(ES6功能方式)

 const isISODate = date => new Date(date) !== "Invalid Date" && !isNaN(new Date(date)) && date == new Date(date).toISOString(); console.log( isISODate("2018-08-01T18:30:00.000Z") ); 

 document.getElementById('r1').innerHTML = dayjs('sdsdsd').isValid() document.getElementById('r2').innerHTML = dayjs('2/6/20').isValid()
 <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script> <p>'sdsdsd' is a date: <span id="r1"></span></p> <p>'2/6/20' is a date: <span id="r2"></span></p>

A light weight library is ready for you: Day.js一个轻量级的库已经为你准备好了: Day.js

This function verifies that the input string in the format m/d/yyyy or mm/dd/yyyy can be converted to a date and that the date is a valid date matching the input string.此函数验证格式为 m/d/yyyy 或 mm/dd/yyyy 的输入字符串是否可以转换为日期,并且该日期是与输入字符串匹配的有效日期。 Add more conditional checks to verify additional formats.添加更多条件检查以验证其他格式。

/**
 * Verify if a string is a date
 * @param {string} sDate - string that should be formatted m/d/yyyy or mm/dd/yyyy
 * @return {boolean} whether string is a valid date
 */
function isValidDate(sDate) {
  let newDate = new Date(sDate);
  console.log(sDate + " date conversion: " + newDate);
  let isDate = (!isNaN(newDate.getTime()));
  console.log(sDate + " is a date: " + isDate);
  if (isDate) {
    let firstSlash = sDate.indexOf("/");
    let secondSlash = sDate.indexOf("/",firstSlash+1);
    let originalDateString = parseInt(sDate.slice(0,firstSlash),10) + "/" + parseInt(sDate.slice(firstSlash+1,secondSlash),10) + "/" + parseInt(sDate.slice(secondSlash+1),10);
    let newDateString = (newDate.getMonth()+1) + "/" + (newDate.getDate()) + "/" + (newDate.getFullYear());
    isDate = (originalDateString == newDateString);
    console.log(originalDateString + " matches " + newDateString + ": " + isDate);
  }
  return isDate;
}

Here is what one could use to validate that the input is a number or a string that can be converted to a date object .这是可以用来验证输入是可以转换为date objectnumberstring的方法。

It covers the following cases:它涵盖以下情况:

  1. catching whatever input leads to "Invalid Date" date constructor result;捕获导致"Invalid Date" date constructor结果的任何输入;
  2. catching the cases where the date is "valid" from technical point of view, but it is not valid from business logic point of view, like从技术角度捕捉日期“有效”但从业务逻辑角度来看无效的情况,例如
  • 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'

What is an easy way to check if a value is a valid date, any known date format allowed.检查值是否为有效日期(允许使用任何已知的日期格式)的简便方法是什么。

For example I have the values 10-11-2009 , 10/11/2009 , 2009-11-10T07:00:00+0000 which should all be recognized as date values, and the values 200 , 10 , 350 , which should not be recognized as a date value.例如我具有值10-11-200910/11/20092009-11-10T07:00:00+0000应该所有被识别为日期值,并且这些值20010350 ,其不应被识别为日期值。 What is the simplest way to check this, if this is even possible?如果可能的话,最简单的检查方法是什么? Because timestamps would also be allowed.因为时间戳也将被允许。

What is an easy way to check if a value is a valid date, any known date format allowed.检查值是否为有效日期(允许使用任何已知的日期格式)的简便方法是什么。

For example I have the values 10-11-2009 , 10/11/2009 , 2009-11-10T07:00:00+0000 which should all be recognized as date values, and the values 200 , 10 , 350 , which should not be recognized as a date value.例如我具有值10-11-200910/11/20092009-11-10T07:00:00+0000应该所有被识别为日期值,并且这些值20010350 ,其不应被识别为日期值。 What is the simplest way to check this, if this is even possible?如果可能的话,最简单的检查方法是什么? Because timestamps would also be allowed.因为时间戳也将被允许。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM