【问题标题】:How to write regex to validate dates?如何编写正则表达式来验证日期?
【发布时间】:2011-04-21 18:45:26
【问题描述】:

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

比赛将是:

dd-mm-yyyy
dd-mm-yy

此外,不应接受前导零,例如:

9-8-2010
10-6-99

如何编写正则表达式来做到这一点?

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    这个怎么样?

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

    我没有测试过这个。这并不能验证闰年

    【讨论】:

    • 这将允许无效日期,例如 31-02-2010
    【解决方案2】:

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

    /^(?:(?: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-190031-12-2099 之间的任何日期都是正确的

    【讨论】:

      【解决方案3】:

      我想出了这个:

      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;
      }
      

      适用于 dd-mm-yyyydd-mm-yyd-m-yyyyd-m-yy,使用 -/ 作为分隔符

      基于This script

      【讨论】:

        【解决方案4】:

        分步解决方案:

        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;
        }
        

        【讨论】:

          【解决方案5】:

          我得到了一些结果来验证 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))$
          

          【讨论】:

            【解决方案6】:

            此变体验证没有分隔符的“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 上测试过)。 ...无论如何,它在本世纪验证了两位数的闰年。

            【讨论】:

              【解决方案7】:

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

              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');
              };
              

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

              这个正则表达式检查:

              • 年份为 20xx;更改为您的偏好。
              • 月、日、小时和分钟的前导零

              此正则表达式不检查:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-11-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-06-22
                • 2011-05-16
                • 2013-04-22
                相关资源
                最近更新 更多