Javascript 如何编写正则表达式来验证日期?

Javascript 如何编写正则表达式来验证日期?,javascript,regex,Javascript,Regex,我正在使用JavaScript,我需要弄清楚如何使用正则表达式确定有效日期 比赛内容如下: dd-mm-yyyy dd-mm-yy 此外,不应接受前导零,如: 9-8-2010 10-6-99 我怎样才能写一个正则表达式来实现这一点呢 [1-9]{1,2}[-./][1-12]{1}[-./](19|20)[1-99]{1} 我还没有测试过这个。而且这不会验证闰年您最好对-进行拆分,并测试所有元素。但是如果你真的想使用正则表达式,你可以试试这个: /^(?:(?:31-(?:(?:0?[1

我正在使用JavaScript,我需要弄清楚如何使用正则表达式确定有效日期

比赛内容如下:

dd-mm-yyyy
dd-mm-yy
此外,不应接受前导零,如:

9-8-2010
10-6-99
我怎样才能写一个正则表达式来实现这一点呢

[1-9]{1,2}[-./][1-12]{1}[-./](19|20)[1-99]{1}

我还没有测试过这个。而且这不会验证闰年

您最好对
-
进行拆分,并测试所有元素。但是如果你真的想使用正则表达式,你可以试试这个:

/^(?:(?:31-(?:(?:0?[13578])|(1[02]))-(19|20)?\d\d)|(?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)|(?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))|(?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d))$/
说明:

^            # start of line
 (?:         # group without capture
             # that match 31st of month 1,3,5,7,8,10,12
   (?:       # group without capture
     31      # number 31
     -       # dash
     (?:     # group without capture
       (?:   # group without capture
         0?  # number 0 optionnal
         [13578] # one digit either 1,3,5,7 or 8
       )     # end group
       |     # alternative
       (1[02]) # 1 followed by 0,1 or 2
     )       # end group
     -       # dash
     (19|20)? #numbers 19 or 20 optionnal
     \d\d    # 2 digits from 00 to 99 
   )         # end group
|
   (?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)
|
   (?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))
|
   (?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d)
 )
$
我已经解释了第一部分,剩下的作为练习

此匹配一个无效日期:
29-02-1900
,但对于
01-01-1900
31-12-2099
之间的任何日期都是正确的

function isValidDate(inputDate){

    var myRegex = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/;
    var match = myRegex.exec(inputDate);

    if (match != null) {
        var auxDay = match[1];
        var auxMonth = match[3] - 1;
        var auxYear = match[4];
        auxYear = auxYear.length < 3 ? (auxYear < 70 ? '20' + auxYear : '19' + auxYear) : auxYear;
        var testingDate = new Date(auxYear,auxMonth,auxDay);
        return ((auxDay == testingDate.getDate()) && (auxMonth == testingDate.getMonth()) && (auxYear == testingDate.getFullYear()));
    } else return false;
}
函数isValidDate(inputDate){
var myRegex=/^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}\d{2})$/;
var match=myRegex.exec(inputDate);
如果(匹配!=null){
var auxDay=match[1];
var auxMonth=匹配[3]-1;
var auxYear=匹配[4];
auxYear=auxYear.length<3?(auxYear<70?'20'+auxYear:'19'+auxYear):auxYear;
var testingDate=新日期(auxYear、auxMonth、auxDay);
返回((auxDay==testingDate.getDate())&&(auxMonth==testingDate.getMonth())&&&(auxYear==testingDate.getFullYear());
}否则返回false;
}
适用于
dd-mm-yyyy
dd-mm-yy
d-m-yyy
d-m-yy
,使用
-
/
作为分隔符

基于逐步解决方案:

function mDateVerify(date) {
    function isLeap(y) {
        return (y % 400 === 0 || y % 100 !== 0) && (y % 4 === 0);
    }
    date = date.match(/^(\d{1,2})-(\d{1,2})-(\d+)$/);
    if (date === null) {
        return false; // if match failed
    }
    var year = parseInt(date[3], 10),
        month = parseInt(date[2], 10),
        day = parseInt(date[1], 10);
    if (month > 12) {
        return false;
    }
    if (month === 2) { // February
        if (isLeap(year)) {
            if (day > 29) {
                return false;
            }
        } else {
            if (day > 28) {
                return false;
            }
        }
    }
    if ((month < 8 && month % 2 === 0) || (month >= 8 && month % 2 === 1)) {
        if (day > 30) {
            return false;
        }
    }
    if (day > 31) {
        return false;
    }
    return true;
}
函数mDateVerify(日期){
函数isLeap(y){
返回值(y%400==0 | | y%100!==0)&(y%4==0);
}
date=date.match(/^(\d{1,2})-(\d{1,2})-(\d+$/);
如果(日期===null){
返回false;//如果匹配失败
}
var年=parseInt(日期[3],10),
月=parseInt(日期[2],10),
day=parseInt(日期[1],10];
如果(月份>12){
返回false;
}
如果(月===2){//2月
如果(isLeap(年)){
如果(第29天){
返回false;
}
}否则{
如果(第28天){
返回false;
}
}
}
如果((月<8和月%2==0)| |(月>=8和月%2==1)){
如果(天>30){
返回false;
}
}
如果(第31天){
返回false;
}
返回true;
}

我得到了一些mm/dd/yyyy格式的验证结果

^(((0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])/(29|30)|(0[13578]|1[02])/31)/(?!0000)[0-9]{4}|02/29/([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00))$
此变体验证不带分隔符的“ddmmyy”

^(?:(?:(?:0[1-9]|1\d|2[0-8])(?:0[1-9]|1[0-2])|(?:29|30)(?:0[13-9]|1[0-2])|31(?:0[13578]|1[02]))\d{2}|2902((?:0[48]|[2468][048]|[13579][26])|00))$
(在debuggex上进行了测试)。
... 无论如何,它在本世纪以两位数验证闰年。

简单地验证yyyy-mm-dd-hh:mm(24小时时间):

将第一项(20{2})更改为\d{4}将允许任何4位数的年份,包括0000和9999。此外,这个正则表达式强制月、日、小时和分钟的前导零

此正则表达式检查:

  • 年份为20xx年;改变你的喜好
  • 月、日、小时和分钟的前导零
此正则表达式不检查:

  • 月/日精度(例如,它将允许2月30日和6月31日)
  • 闰年
  • 上午或下午(24小时)

这将允许无效日期,如2010年2月31日
var dateFormat=/^20\d{2}-(0[1-9]|1[0-2])-[0-3]\d\s([0-1][0-9]|2[0-3]):[0-5]\d$/;
var myDate="2017-12-31"; 
if ( myDate.match(dateFormat)){
    console.log('matches');
};