【问题标题】:How to validate date with format "mm/dd/yyyy" in JavaScript?如何在 JavaScript 中使用“mm/dd/yyyy”格式验证日期?
【发布时间】:2011-09-04 21:34:40
【问题描述】:

我想使用mm/dd/yyyy 格式验证输入的日期格式。

我在一个站点中找到了以下代码,然后使用它,但它不起作用:

function isDate(ExpiryDate) { 
    var objDate,  // date object initialized from the ExpiryDate string 
        mSeconds, // ExpiryDate in milliseconds 
        day,      // day 
        month,    // month 
        year;     // year 
    // date length should be 10 characters (no more no less) 
    if (ExpiryDate.length !== 10) { 
        return false; 
    } 
    // third and sixth character should be '/' 
    if (ExpiryDate.substring(2, 3) !== '/' || ExpiryDate.substring(5, 6) !== '/') { 
        return false; 
    } 
    // extract month, day and year from the ExpiryDate (expected format is mm/dd/yyyy) 
    // subtraction will cast variables to integer implicitly (needed 
    // for !== comparing) 
    month = ExpiryDate.substring(0, 2) - 1; // because months in JS start from 0 
    day = ExpiryDate.substring(3, 5) - 0; 
    year = ExpiryDate.substring(6, 10) - 0; 
    // test year range 
    if (year < 1000 || year > 3000) { 
        return false; 
    } 
    // convert ExpiryDate to milliseconds 
    mSeconds = (new Date(year, month, day)).getTime(); 
    // initialize Date() object from calculated milliseconds 
    objDate = new Date(); 
    objDate.setTime(mSeconds); 
    // compare input date and parts from Date() object 
    // if difference exists then date isn't valid 
    if (objDate.getFullYear() !== year || 
        objDate.getMonth() !== month || 
        objDate.getDate() !== day) { 
        return false; 
    } 
    // otherwise return true 
    return true; 
}

function checkDate(){ 
    // define date string to test 
    var ExpiryDate = document.getElementById(' ExpiryDate').value; 
    // check date and print message 
    if (isDate(ExpiryDate)) { 
        alert('OK'); 
    } 
    else { 
        alert('Invalid date format!'); 
    } 
}

有什么可能出错的建议吗?

【问题讨论】:

  • 欢迎来到 StackOverflow。您可以使用{} 工具栏按钮格式化源代码。这次我已经为你做了。此外,请尝试提供有关您的问题的一些信息:不起作用描述作为然后修复它解决方案很有用。
  • 您要验证哪种日期格式?你能举一些应该有效的日期的例子吗?

标签: javascript validation date


【解决方案1】:

我认为 Niklas 对您的问题有正确的答案。除此之外,我认为下面的日期验证函数更容易阅读:

// Validates that the input string is a valid date formatted as "mm/dd/yyyy"
function isValidDate(dateString)
{
    // First check for the pattern
    if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))
        return false;

    // Parse the date parts to integers
    var parts = dateString.split("/");
    var day = parseInt(parts[1], 10);
    var month = parseInt(parts[0], 10);
    var year = parseInt(parts[2], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
        return false;

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        monthLength[1] = 29;

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
};

【讨论】:

  • 记得使用 parseInt 的第二个参数:parseInt(parts[0], 10)。否则,九月的 09 被读取为八进制并解析为 0
  • 几年下来,这为我节省了很多时间,感谢您的甜蜜回答!
  • 优秀的帖子!将正则表达式格式与验证所需的解析相结合。
  • 我建议您将正则表达式更改为:/^(\d{2}|\d{1})\/(\d{2}|\d{1})\/ \d{4}$/ 这样它可以捕获 2014 年 1 月 5 日的一位数月份和日期。感谢您的样品!
  • @MitchLabrador - 感谢您的反馈。我根据您的建议改进了正则表达式。
【解决方案2】:

我会使用Moment.js 进行日期验证。

alert(moment("05/22/2012", 'MM/DD/YYYY',true).isValid()); //true

Jsfiddle:http://jsfiddle.net/q8y9nbu5/

true 值用于严格解析归功于@Andrey Prokhorov,这意味着

您可以为最后一个参数指定一个布尔值以使 Moment 使用 严格解析。严格解析要求格式和输入 完全匹配,包括分隔符。

【讨论】:

  • 使用“M/D/YYYY”允许 1-2 位数字表示月份和日期。
  • 很高兴知道第三个参数“true”保留为“使用严格解析”momentjs.com/docs/#/parsing/string-format
  • @Razan Paul 希望您不介意我添加了一些解释以更清晰。明智的做法是不要一次又一次地重新发明轮子,所以 pual 的答案在我看来是最好的答案
  • moment(dateString, 'MM/DD/YYYY', true).isValid() || moment(dateString, 'M/DD/YYYY', true).isValid() || moment(dateString, 'MM/D/YYYY', true).isValid();
【解决方案3】:

使用以下正则表达式进行验证:

var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
if (!(date_regex.test(testDate))) {
    return false;
}

这对我来说适用于 MM/dd/yyyy。

【讨论】:

  • 我们将如何验证 yyyy-mm-dd 或无效日期,例如 9834-66-43
  • 你可以使用 /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2 ][0-9]|3[0-1])$/ 来验证 yyyy-mm-dd。
  • 这太棒了,因为我一个讨厌制定正则表达式,两个喜欢他们的效率!
  • 3000 年会发生什么? :)
  • @TheOne..y3k 问题.. :P
【解决方案4】:

All credits go to elian-ebbing

这里我也为懒人提供了一个自定义版本的函数,格式为yyyy-mm-dd。

function isValidDate(dateString)
{
    // First check for the pattern
    var regex_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/;

    if(!regex_date.test(dateString))
    {
        return false;
    }

    // Parse the date parts to integers
    var parts   = dateString.split("-");
    var day     = parseInt(parts[2], 10);
    var month   = parseInt(parts[1], 10);
    var year    = parseInt(parts[0], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
    {
        return false;
    }

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    {
        monthLength[1] = 29;
    }

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
}

【讨论】:

  • 这将验证 '2020-5-1' 为真,而忽略前导零。在解析之前,我首先使用/^(19|20)\d\d$/ 测试年份模式,使用/^(0[0-9]|1[0-2])$/ 测试月份模式,使用/^(0[1-9]|[12][0-9]|3[01])$/ 测试日期模式。然后它工作了谢谢。
  • 为了准确地测试日期的模式 yyyy-mm-dd 格式,这个正则表达式 /^\d{4}\-\d{1,2}\-\d{1,2}$/ 将验证 yyyy-mm-dd 或 yyyy-md 为真,因此它只验证长度不每个单独的日期部分。对于 yyyy-mm-dd 的精确长度而不检查年、月和日期是否正确,请改用/^\d{4}\-\d{2}\-\d{2}$/。
【解决方案5】:

你可以使用Date.parse()

您可以在MDN documentation阅读

Date.parse() 方法解析日期的字符串表示,并且 返回自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数 或 NaN 如果字符串无法识别,或者在某些情况下包含 非法日期值(例如 2015-02-31)。

并检查Date.parse的结果是否为NaN

let isValidDate = Date.parse('01/29/1980');

if (isNaN(isValidDate)) {
  // when is not valid date logic

  return false;
}

// when is valid date logic

请看看MDN什么时候推荐使用Date.parse

【讨论】:

  • Date.parse 会给你一个有效的解析,比如“46/7/17”
  • 将返回 yyyy/02/30 的真实结果
  • 投反对票,因为这将允许垃圾通过。例如Date.parse('01//////29//000')
【解决方案6】:

看到这么老的关于这样一个基本主题的帖子,有这么多答案,但没有一个是正确的,这是不寻常的。 (我并不是说它们都不起作用。)

  • 为此不需要闰年确定例程。语言可以为我们做到这一点。
  • 不需要时间。
  • Date.parse() 不应用于本地日期字符串。 MDN 说“在 ES5 之前不建议使用 Date.parse,字符串的解析完全依赖于实现。”该标准需要一个(可能简化的)ISO 8601 字符串;对任何其他格式的支持取决于实现。
  • 也不应该使用new Date(string),因为它使用了Date.parse()。
  • 应验证 IMO 闰日。
  • 验证函数必须考虑输入字符串与预期格式不匹配的可能性。例如,“1a/2a/3aaa”、“1234567890”或“ab/cd/efgh”。

这是一个高效、简洁的解决方案,没有隐式转换。它利用了 Date 构造函数将 2018-14-29 解释为 2019-03-01 的意愿。它确实使用了一些现代语言功能,但如果需要,这些功能很容易删除。我还包括了一些测试。

function isValidDate(s) {
    // Assumes s is "mm/dd/yyyy"
    if ( ! /^\d\d\/\d\d\/\d\d\d\d$/.test(s) ) {
        return false;
    }
    const parts = s.split('/').map((p) => parseInt(p, 10));
    parts[0] -= 1;
    const d = new Date(parts[2], parts[0], parts[1]);
    return d.getMonth() === parts[0] && d.getDate() === parts[1] && d.getFullYear() === parts[2];
}

function testValidDate(s) {
    console.log(s, isValidDate(s));
}
testValidDate('01/01/2020'); // true
testValidDate('02/29/2020'); // true
testValidDate('02/29/2000'); // true
testValidDate('02/29/1900'); // false
testValidDate('02/29/2019'); // false
testValidDate('01/32/1970'); // false
testValidDate('13/01/1970'); // false
testValidDate('14/29/2018'); // false
testValidDate('1a/2b/3ccc'); // false
testValidDate('1234567890'); // false
testValidDate('aa/bb/cccc'); // false
testValidDate(null);         // false
testValidDate('');           // false

【讨论】:

  • 干得好!!其中确实需要更多关注..
  • 不错的答案。可以使用const [mm, dd, yyyy] = date.split('/').map((p) =&gt; parseInt(p)); 而不是parts[x] 来提高可读性
【解决方案7】:

它似乎适用于 mm/dd/yyyy 格式的日期,例如:

http://jsfiddle.net/niklasvh/xfrLm/

我对您的代码的唯一问题是:

var ExpiryDate = document.getElementById(' ExpiryDate').value;

在元素 ID 之前的括号内有一个空格。改为:

var ExpiryDate = document.getElementById('ExpiryDate').value;

没有关于无效数据类型的任何进一步详细信息,没有太多其他可提供的输入。

【讨论】:

    【解决方案8】:

    如果给定的字符串格式正确('MM/DD/YYYY'),该函数将返回 true,否则将返回 false。 (我在网上找到了这段代码并稍作修改)

    function isValidDate(date) {
        var temp = date.split('/');
        var d = new Date(temp[2] + '/' + temp[0] + '/' + temp[1]);
        return (d && (d.getMonth() + 1) == temp[0] && d.getDate() == Number(temp[1]) && d.getFullYear() == Number(temp[2]));
    }
    
    console.log(isValidDate('02/28/2015'));
                

    【讨论】:

      【解决方案9】:

      这是一个用于检查有效日期的 sn-p:

      function validateDate(dateStr) {
         const regExp = /^(\d\d?)\/(\d\d?)\/(\d{4})$/;
         let matches = dateStr.match(regExp);
         let isValid = matches;
         let maxDate = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
         
         if (matches) {
           const month = parseInt(matches[1]);
           const date = parseInt(matches[2]);
           const year = parseInt(matches[3]);
           
           isValid = month <= 12 && month > 0;
           isValid &= date <= maxDate[month] && date > 0;
           
           const leapYear = (year % 400 == 0)
              || (year % 4 == 0 && year % 100 != 0);
           isValid &= month != 2 || leapYear || date <= 28; 
         }
         
         return isValid
      }
      
      console.log(['1/1/2017', '01/1/2017', '1/01/2017', '01/01/2017', '13/12/2017', '13/13/2017', '12/35/2017'].map(validateDate));

      【讨论】:

        【解决方案10】:

        如果要检查验证dd/MM/yyyy也没关系

        function isValidDate(date) {
            var temp = date.split('/');
            var d = new Date(temp[1] + '/' + temp[0] + '/' + temp[2]);
             return (d && (d.getMonth() + 1) == temp[1] && d.getDate() == Number(temp[0]) && d.getFullYear() == Number(temp[2]));
        }
        
        alert(isValidDate('29/02/2015')); // it not exist ---> false
                    

        【讨论】:

          【解决方案11】:

          在下面的代码中查找可以对任何提供的格式执行日期验证,以验证开始/开始和结束/结束日期。可能有一些更好的方法,但已经想出了这个。 注意提供的日期格式和日期字符串齐头并进。

          <script type="text/javascript">
              function validate() {
          
                  var format = 'yyyy-MM-dd';
          
                  if(isAfterCurrentDate(document.getElementById('start').value, format)) {
                      alert('Date is after the current date.');
                  } else {
                      alert('Date is not after the current date.');
                  }
                  if(isBeforeCurrentDate(document.getElementById('start').value, format)) {
                      alert('Date is before current date.');
                  } else {
                      alert('Date is not before current date.');
                  }
                  if(isCurrentDate(document.getElementById('start').value, format)) {
                      alert('Date is current date.');
                  } else {
                      alert('Date is not a current date.');
                  }
                  if (isBefore(document.getElementById('start').value, document.getElementById('end').value, format)) {
                      alert('Start/Effective Date cannot be greater than End/Expiration Date');
                  } else {
                      alert('Valid dates...');
                  }
                  if (isAfter(document.getElementById('start').value, document.getElementById('end').value, format)) {
                      alert('End/Expiration Date cannot be less than Start/Effective Date');
                  } else {
                      alert('Valid dates...');
                  }
                  if (isEquals(document.getElementById('start').value, document.getElementById('end').value, format)) {
                      alert('Dates are equals...');
                  } else {
                      alert('Dates are not equals...');
                  }
                  if (isDate(document.getElementById('start').value, format)) {
                      alert('Is valid date...');
                  } else {
                      alert('Is invalid date...');
                  }
              }
          
              /**
               * This method gets the year index from the supplied format
               */
              function getYearIndex(format) {
          
                  var tokens = splitDateFormat(format);
          
                  if (tokens[0] === 'YYYY'
                          || tokens[0] === 'yyyy') {
                      return 0;
                  } else if (tokens[1]=== 'YYYY'
                          || tokens[1] === 'yyyy') {
                      return 1;
                  } else if (tokens[2] === 'YYYY'
                          || tokens[2] === 'yyyy') {
                      return 2;
                  }
                  // Returning the default value as -1
                  return -1;
              }
          
              /**
               * This method returns the year string located at the supplied index
               */
              function getYear(date, index) {
          
                  var tokens = splitDateFormat(date);
                  return tokens[index];
              }
          
              /**
               * This method gets the month index from the supplied format
               */
              function getMonthIndex(format) {
          
                  var tokens = splitDateFormat(format);
          
                  if (tokens[0] === 'MM'
                          || tokens[0] === 'mm') {
                      return 0;
                  } else if (tokens[1] === 'MM'
                          || tokens[1] === 'mm') {
                      return 1;
                  } else if (tokens[2] === 'MM'
                          || tokens[2] === 'mm') {
                      return 2;
                  }
                  // Returning the default value as -1
                  return -1;
              }
          
              /**
               * This method returns the month string located at the supplied index
               */
              function getMonth(date, index) {
          
                  var tokens = splitDateFormat(date);
                  return tokens[index];
              }
          
              /**
               * This method gets the date index from the supplied format
               */
              function getDateIndex(format) {
          
                  var tokens = splitDateFormat(format);
          
                  if (tokens[0] === 'DD'
                          || tokens[0] === 'dd') {
                      return 0;
                  } else if (tokens[1] === 'DD'
                          || tokens[1] === 'dd') {
                      return 1;
                  } else if (tokens[2] === 'DD'
                          || tokens[2] === 'dd') {
                      return 2;
                  }
                  // Returning the default value as -1
                  return -1;
              }
          
              /**
               * This method returns the date string located at the supplied index
               */
              function getDate(date, index) {
          
                  var tokens = splitDateFormat(date);
                  return tokens[index];
              }
          
              /**
               * This method returns true if date1 is before date2 else return false
               */
              function isBefore(date1, date2, format) {
                  // Validating if date1 date is greater than the date2 date
                  if (new Date(getYear(date1, getYearIndex(format)), 
                          getMonth(date1, getMonthIndex(format)) - 1, 
                          getDate(date1, getDateIndex(format))).getTime()
                      > new Date(getYear(date2, getYearIndex(format)), 
                          getMonth(date2, getMonthIndex(format)) - 1, 
                          getDate(date2, getDateIndex(format))).getTime()) {
                      return true;
                  } 
                  return false;                
              }
          
              /**
               * This method returns true if date1 is after date2 else return false
               */
              function isAfter(date1, date2, format) {
                  // Validating if date2 date is less than the date1 date
                  if (new Date(getYear(date2, getYearIndex(format)), 
                          getMonth(date2, getMonthIndex(format)) - 1, 
                          getDate(date2, getDateIndex(format))).getTime()
                      < new Date(getYear(date1, getYearIndex(format)), 
                          getMonth(date1, getMonthIndex(format)) - 1, 
                          getDate(date1, getDateIndex(format))).getTime()
                      ) {
                      return true;
                  } 
                  return false;                
              }
          
              /**
               * This method returns true if date1 is equals to date2 else return false
               */
              function isEquals(date1, date2, format) {
                  // Validating if date1 date is equals to the date2 date
                  if (new Date(getYear(date1, getYearIndex(format)), 
                          getMonth(date1, getMonthIndex(format)) - 1, 
                          getDate(date1, getDateIndex(format))).getTime()
                      === new Date(getYear(date2, getYearIndex(format)), 
                          getMonth(date2, getMonthIndex(format)) - 1, 
                          getDate(date2, getDateIndex(format))).getTime()) {
                      return true;
                  } 
                  return false;
              }
          
              /**
               * This method validates and returns true if the supplied date is 
               * equals to the current date.
               */
              function isCurrentDate(date, format) {
                  // Validating if the supplied date is the current date
                  if (new Date(getYear(date, getYearIndex(format)), 
                          getMonth(date, getMonthIndex(format)) - 1, 
                          getDate(date, getDateIndex(format))).getTime()
                      === new Date(new Date().getFullYear(), 
                              new Date().getMonth(), 
                              new Date().getDate()).getTime()) {
                      return true;
                  } 
                  return false;                
              }
          
              /**
               * This method validates and returns true if the supplied date value 
               * is before the current date.
               */
              function isBeforeCurrentDate(date, format) {
                  // Validating if the supplied date is before the current date
                  if (new Date(getYear(date, getYearIndex(format)), 
                          getMonth(date, getMonthIndex(format)) - 1, 
                          getDate(date, getDateIndex(format))).getTime()
                      < new Date(new Date().getFullYear(), 
                              new Date().getMonth(), 
                              new Date().getDate()).getTime()) {
                      return true;
                  } 
                  return false;                
              }
          
              /**
               * This method validates and returns true if the supplied date value 
               * is after the current date.
               */
              function isAfterCurrentDate(date, format) {
                  // Validating if the supplied date is before the current date
                  if (new Date(getYear(date, getYearIndex(format)), 
                          getMonth(date, getMonthIndex(format)) - 1, 
                          getDate(date, getDateIndex(format))).getTime()
                      > new Date(new Date().getFullYear(),
                              new Date().getMonth(), 
                              new Date().getDate()).getTime()) {
                      return true;
                  } 
                  return false;                
              }
          
              /**
               * This method splits the supplied date OR format based 
               * on non alpha numeric characters in the supplied string.
               */
              function splitDateFormat(dateFormat) {
                  // Spliting the supplied string based on non characters
                  return dateFormat.split(/\W/);
              }
          
              /*
               * This method validates if the supplied value is a valid date.
               */
              function isDate(date, format) {                
                  // Validating if the supplied date string is valid and not a NaN (Not a Number)
                  if (!isNaN(new Date(getYear(date, getYearIndex(format)), 
                          getMonth(date, getMonthIndex(format)) - 1, 
                          getDate(date, getDateIndex(format))))) {                    
                      return true;
                  } 
                  return false;                                      
              }
          </script>
          

          下面是 HTML sn-p

          <input type="text" name="start" id="start" size="10" value="" />
          <br/>
          <input type="text" name="end" id="end" size="10" value="" />
          <br/>
          <input type="button" value="Submit" onclick="javascript:validate();" />
          

          【讨论】:

          • 优秀。这就是我一直在寻找的。​​span>
          【解决方案12】:

          另类

          这个问题已经得到了令人作呕的回答,我知道,但我想通过提出替代解决方案来投身于 SO 狼群。

          AFAICT,此方法未出现在任何其他答案中。也许还有一些我没有考虑过的极端情况......我相信如果是这样的话,你们都会适当地烤我。

          有人曾经说过——也许是耶稣或孔子——

          当您决定使用正则表达式解决问题时, 现在你有两个问题

          也就是说,我喜欢正则表达式,并经常将它用于其他事情;但是,最近,我在这里遇到了一个热门答案的问题,它对无效日期抛出了误报。它使用了过于严格的正则表达式。我用以下内容替换了对日期字符串 (dd/dd/dddd) 的所述检查:

          const isInvalidDate = (dateString) => new Date(dateString).toString() === 'Invalid Date'
          
          console.log(isInvalidDate('asdf')) //> true
          console.log(isInvalidDate('09/29/1985')) //> false
          

          【讨论】:

            【解决方案13】:

            我从here 的另一个帖子中提取了大部分代码。我已根据我的目的对其进行了修改。这很适合我的需要。它可能对您的情况有所帮助。

            $(window).load(function() {
              function checkDate() {
                var dateFormat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
                var valDate = $(this).val();
                if ( valDate.match( dateFormat )) {
                  $(this).css("border","1px solid #cccccc","color", "#555555", "font-weight", "normal");
                  var seperator1 = valDate.split('/');
                  var seperator2 = valDate.split('-');
            
                  if ( seperator1.length > 1 ) {
                    var splitdate = valDate.split('/');
                  } else if ( seperator2.length > 1 ) {
                    var splitdate = valDate.split('-');
                  }
            
                  var dd = parseInt(splitdate[0]);
                  var mm = parseInt(splitdate[1]);
                  var yy = parseInt(splitdate[2]);
                  var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31];
            
                  if ( mm == 1 || mm > 2 ) {
                    if ( dd > ListofDays[mm - 1] ) {
                      $(this).val("");
                      $(this).css("border","solid red 1px","color", "red", "font-weight", "bold");
                      alert('Invalid Date! You used a date which does not exist in the known calender.');
                      return false;
                    }
                  }
            
                  if ( mm == 2 ) {
                   var lyear = false;
                    if ( (!(yy % 4) && yy % 100) || !(yy % 400) ){
                      lyear = true;
                    }
            
                    if ( (lyear==false) && (dd>=29) ) {
                      $(this).val("");
                      $(this).css("border","solid red 1px","color", "red", "font-weight", "bold");
                      alert('Invalid Date! You used Feb 29th for an invalid leap year');
                      return false;
                    }
            
                    if ( (lyear==true) && (dd>29) ) {
                      $(this).val("");
                      $(this).css("border","solid red 1px","color", "red", "font-weight", "bold");
                      alert('Invalid Date! You used a date greater than Feb 29th in a valid leap year');
                      return false;
                    }
                 }
                } else {
                  $(this).val("");
                  $(this).css("border","solid red 1px","color", "red", "font-weight", "bold");
                  alert('Date format was invalid! Please use format mm/dd/yyyy');
                  return false;
                }
              };
            
              $('#from_date').change( checkDate );
              $('#to_date').change( checkDate );
            });
            

            【讨论】:

              【解决方案14】:

              与 Elian Ebbing 答案类似,但支持“\”、“/”、“.”、“-”、“” 分隔符

              function js_validate_date_dmyyyy(js_datestr)
              {
                  var js_days_in_year = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
                  var js_datepattern = /^(\d{1,2})([\.\-\/\\ ])(\d{1,2})([\.\-\/\\ ])(\d{4})$/;
              
                  if (! js_datepattern.test(js_datestr)) { return false; }
              
                  var js_match = js_datestr.match(js_datepattern);
                  var js_day = parseInt(js_match[1]);
                  var js_delimiter1 = js_match[2];
                  var js_month = parseInt(js_match[3]);
                  var js_delimiter2 = js_match[4];
                  var js_year = parseInt(js_match[5]);                            
              
                  if (js_is_leap_year(js_year)) { js_days_in_year[2] = 29; }
              
                  if (js_delimiter1 !== js_delimiter2) { return false; } 
                  if (js_month === 0  ||  js_month > 12)  { return false; } 
                  if (js_day === 0  ||  js_day > js_days_in_year[js_month])   { return false; } 
              
                  return true;
              }
              
              function js_is_leap_year(js_year)
              { 
                  if(js_year % 4 === 0)
                  { 
                      if(js_year % 100 === 0)
                      { 
                          if(js_year % 400 === 0)
                          { 
                              return true; 
                          } 
                          else return false; 
                      } 
                      else return true; 
                  } 
                  return false; 
              }
              

              【讨论】:

              • 你的日子和月份倒退了。
              【解决方案15】:
              function fdate_validate(vi)
              {
                var parts =vi.split('/');
                var result;
                var mydate = new Date(parts[2],parts[1]-1,parts[0]);
                if (parts[2] == mydate.getYear() && parts[1]-1 == mydate.getMonth() && parts[0] == mydate.getDate() )
                {result=0;}
                else
                {result=1;}
                return(result);
              }
              

              【讨论】:

              • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
              【解决方案16】:

              Moment 确实是解决问题的好方法。我看不出有理由仅仅为了检查日期而增加复杂性......看看时刻:http://momentjs.com/

              HTML:

              <input class="form-control" id="date" name="date" onchange="isValidDate(this);" placeholder="DD/MM/YYYY" type="text" value="">
              

              脚本:

               function isValidDate(dateString)  {
                  var dateToValidate = dateString.value
                  var isValid = moment(dateToValidate, 'MM/DD/YYYY',true).isValid()
                  if (isValid) {
                      dateString.style.backgroundColor = '#FFFFFF';
                  } else {
                      dateString.style.backgroundColor = '#fba';
                  }   
              };
              

              【讨论】:

                【解决方案17】:

                在@Jay Dunnings 答案上扩展(或更好地收缩),以下双线 使用Intl API 和ES6 解构。它通过了 Jay Dunning 的所有测试。

                /**
                 * @param {string} date
                 * @returns {boolean} true if date is of the form mm/dd/yyyy
                 */
                function is_en_US_date(date) {
                    const [match, mm, dd, yyyy] = /^(\d\d)[/](\d\d)[/](\d{4})$/.exec(date) || [];
                    return match !== undefined && new Intl.DateTimeFormat('en-US', { month: '2-digit', day: '2-digit', year: 'numeric' }).format(new Date(yyyy, mm-1, dd)) === date
                }
                

                【讨论】:

                  【解决方案18】:
                  1. Javascript

                    function validateDate(date) {
                        try {
                            new Date(date).toISOString();
                            return true;
                        } catch (e) { 
                            return false; 
                        }
                    }
                    
                  2. jQuery

                    $.fn.validateDate = function() {
                        try {
                            new Date($(this[0]).val()).toISOString();
                            return true;
                        } catch (e) { 
                            return false; 
                        }
                    }
                    

                  对于有效的日期字符串返回 true。

                  【讨论】:

                    【解决方案19】:

                    我们可以使用自定义函数或日期模式。以下代码是自定义功能,请根据您的要求进行更改。

                     function isValidDate(str, separator = '-') {
                        var getvalue = str.split(separator);
                        var day = parseInt(getvalue[2]);
                        var month = parseInt(getvalue[1]);
                        var year = parseInt(getvalue[0]);
                        if(year < 1901 || year > 2100){
                           return false;
                        }
                        if (month < 1 || month > 12) { 
                           return false;
                        }
                        if (day < 1 || day > 31) {
                           return false;
                        }
                        if ((month==4 && month==6 && month==9 && month==11) && day==31) {
                           return false;
                        }
                        if (month == 2) { // check for february 29th
                           var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                           if (day>29 || (day==29 && !isleap)) {
                              return false;
                           }
                        }
                        else{
                           return true;
                        }
                        return true;
                    }
                    

                    【讨论】:

                      【解决方案20】:

                      日期很复杂。 验证它的最佳方法是使用 Luxon、date-fns 或 DayJS 之类的包。

                      使用日期-fns:

                      import {isMatch} from 'date-fns'
                      
                      const match = isMatch(new Date('12/25/2010'), 'MM/dd/YYYY') // true
                      

                      【讨论】:

                        【解决方案21】:
                        function validatedate(inputText, DateFormat) {
                            // format dd/mm/yyyy or in any order of (dd or mm or yyyy) you can write dd or mm or yyyy in first or second or third position ... or can be slash"/" or dot"." or dash"-" in the dates formats
                            var invalid = "";
                            var dt = "";
                            var mn = "";
                            var yr = "";
                            var k;
                            var delm = DateFormat.includes("/") ? "/" : ( DateFormat.includes("-") ? "-" : ( DateFormat.includes(".") ? "." : "" ) ) ;
                            var f1 = inputText.split(delm);
                            var f2 = DateFormat.split(delm);
                            for(k=0;k<=2;k++) { 
                                dt = dt + (f2[parseInt(k)]=="dd" ? f1[parseInt(k)] : "");
                                mn = mn + (f2[parseInt(k)]=="mm" ? f1[parseInt(k)] : "");
                                yr = yr + (f2[parseInt(k)]=="yyyy" ? f1[parseInt(k)] : "");
                            }
                            var mn_days = "0-31-" + (yr % 4 == 0 ? 29 : 28) + "-31-30-31-30-31-31-30-31-30-31";
                            var days = mn_days.split("-");
                            if (f1.length!=3 ||
                            mn.length>2 ||
                            dt.length>2 ||
                            yr.length!=4 ||
                            !(parseInt(mn)>=1 && parseInt(mn)<=12) ||
                            !(parseInt(yr)>=parseInt(1900) && parseInt(yr)<=parseInt(2100)) ||
                            !(parseInt(dt)>=1 && parseInt(dt)<=parseInt(days[parseInt(mn)]))) {
                                invalid = "true";
                            }
                            alert( ( invalid=="true" ? "Invalid Date" : "Valid Date")  );
                        }
                        

                        【讨论】:

                        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
                        【解决方案22】:

                        先将字符串日期转成js日期格式,再转成字符串格式,再与原字符串比较。

                        function dateValidation(){
                            var dateString = "34/05/2019"
                            var dateParts = dateString.split("/");
                            var date= new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
                        
                            var isValid = isValidDate( dateString, date );
                            console.log("Is valid date: " + isValid);
                        }
                        
                        function isValidDate(dateString, date) {
                            var newDateString = ( date.getDate()<10 ? ('0'+date.getDate()) : date.getDate() )+ '/'+ ((date.getMonth() + 1)<10? ('0'+(date.getMonth() + 1)) : (date.getMonth() + 1) )  + '/' +  date.getFullYear();
                            return ( dateString == newDateString);
                        }
                        

                        【讨论】:

                        • 这不会运行,因为 ìsValid` 不是一个函数。
                        【解决方案23】:

                        function validatedate(inputText, dateFormat) {
                          var minYear = 1950;
                          var maxYear = 2050;
                          inputText = inputText.replace("A", "a");
                          inputText = inputText.replace("B", "b");
                          inputText = inputText.replace("C", "c");
                          inputText = inputText.replace("D", "d");
                          inputText = inputText.replace("E", "e");
                          inputText = inputText.replace("F", "f");
                          inputText = inputText.replace("G", "g");
                          inputText = inputText.replace("H", "h");
                          inputText = inputText.replace("I", "i");
                          inputText = inputText.replace("J", "j");
                          inputText = inputText.replace("K", "k");
                          inputText = inputText.replace("L", "l");
                          inputText = inputText.replace("M", "m");
                          inputText = inputText.replace("O", "o");
                          inputText = inputText.replace("P", "p");
                          inputText = inputText.replace("Q", "q");
                          inputText = inputText.replace("R", "r");
                          inputText = inputText.replace("S", "s");
                          inputText = inputText.replace("T", "t");
                          inputText = inputText.replace("U", "u");
                          inputText = inputText.replace("V", "v");
                          inputText = inputText.replace("W", "w");
                          inputText = inputText.replace("X", "x");
                          inputText = inputText.replace("Y", "y");
                          inputText = inputText.replace("Z", "z");
                          if (dateFormat == "dd/mmm/yyyy" || dateFormat == "dd-mmm-yyyy" || dateFormat == "dd.mmm.yyyy" || dateFormat == "dd/yyyy/mmm" || dateFormat == "dd-yyyy-mmm" || dateFormat == "dd.yyyy.mmm" || dateFormat == "mmm/dd/yyyy" || dateFormat == "mmm-dd-yyyy" || dateFormat == "mmm.dd.yyyy" || dateFormat == "mmm/yyyy/dd" || dateFormat == "mmm-yyyy-dd" || dateFormat == "mmm.yyyy.dd" || dateFormat == "yyyy/mmm/dd" || dateFormat == "yyyy-mmm-dd" || dateFormat == "yyyy.mmm.dd" || dateFormat == "yyyy/dd/mmm" || dateFormat == "yyyy-dd-mmm" || dateFormat == "yyyy.dd.mmm") {
                            dateFormat = dateFormat.replace("mmm", "mm");
                            inputText = inputText.replace("jan", "01");
                            inputText = inputText.replace("feb", "02");
                            inputText = inputText.replace("mar", "03");
                            inputText = inputText.replace("apr", "04");
                            inputText = inputText.replace("may", "05");
                            inputText = inputText.replace("jun", "06");
                            inputText = inputText.replace("jul", "07");
                            inputText = inputText.replace("aug", "08");
                            inputText = inputText.replace("sep", "09");
                            inputText = inputText.replace("oct", "10");
                            inputText = inputText.replace("nov", "11");
                            inputText = inputText.replace("dec", "12");
                          }
                        
                        
                          var w;
                          var q;
                          var delm;
                          delm1 = "/";
                        
                          for (w = 0; w < inputText.length; w++) {
                            q = inputText.charAt(w);
                            if (q == '0' || q == '1' || q == '2' || q == '3' || q == '4' || q == '5' || q == '6' || q == '7' || q == '8' || q == '9' || q == '/') {} else {
                              delm1 = "";
                            }
                          }
                          delm2 = "-";
                        
                          for (w = 0; w < inputText.length; w++) {
                            q = inputText.charAt(w);
                            if (q == '0' || q == '1' || q == '2' || q == '3' || q == '4' || q == '5' || q == '6' || q == '7' || q == '8' || q == '9' || q == '-') {} else {
                              delm2 = "";
                            }
                          }
                        
                          delm3 = ".";
                        
                          for (w = 0; w < inputText.length; w++) {
                            q = inputText.charAt(w);
                            if (q == '0' || q == '1' || q == '2' || q == '3' || q == '4' || q == '5' || q == '6' || q == '7' || q == '8' || q == '9' || q == '.') {} else {
                              delm3 = "";
                            }
                          }
                        
                          var delm;
                          if (delm1 == "/" && (dateFormat == "dd/mm/yyyy" || dateFormat == "mm/dd/yyyy" || dateFormat == "dd/yyyy/mm" || dateFormat == "mm/yyyy/dd" || dateFormat == "yyyy/mm/dd" || dateFormat == "yyyy/dd/mm")) {
                            delm = "/";
                          }
                        
                          if (delm2 == "-" && (dateFormat == "dd-mm-yyyy" || dateFormat == "mm-dd-yyyy" || dateFormat == "dd-yyyy-mm" || dateFormat == "mm-yyyy-dd" || dateFormat == "yyyy-mm-dd" || dateFormat == "yyyy-dd-mm")) {
                            delm = "-";
                          }
                          if (delm3 == "." && (dateFormat == "dd.mm.yyyy" || dateFormat == "mm.dd.yyyy" || dateFormat == "dd.yyyy.mm" || dateFormat == "mm.yyyy.dd" || dateFormat == "yyyy.mm.dd" || dateFormat == "yyyy.dd.mm")) {
                            delm = ".";
                          }
                        
                          var invalid;
                          var f = "31/12/2000";
                          f = inputText;
                          var ln = f.length;
                          var dt;
                          var mn;
                          var yr;
                          var t = f.split(delm);
                          var j = t.length;
                          if (j == 3) {
                            dt = t[0];
                            mn = t[1];
                            yr = t[2];
                        
                            if (dateFormat == "mm.dd.yyyy" || dateFormat == "mm/dd/yyyy" || dateFormat == "mm-dd-yyyy") {
                              var tmp = mn;
                              mn = dt;
                              dt = tmp;
                            }
                        
                            if (dateFormat == "dd.yyyy.mm" || dateFormat == "dd/yyyy/mm" || dateFormat == "dd-yyyy-mm") {
                              var tmp = mn;
                              mn = yr;
                              yr = tmp;
                            }
                            if (dateFormat == "mm.yyyy.dd" || dateFormat == "mm/yyyy/dd" || dateFormat == "mm-yyyy-dd") {
                              var m1 = mn;
                              var d1 = dt;
                              var y1 = yr;
                              mn = d1;
                              yr = m1;
                              dt = y1;
                            }
                        
                            if (dateFormat == "yyyy.mm.dd" || dateFormat == "yyyy/mm/dd" || dateFormat == "yyyy-mm-dd") {
                              var m1 = mn;
                              var d1 = dt;
                              var y1 = yr;
                              mn = m1;
                              yr = d1;
                              dt = y1;
                            }
                        
                            if (dateFormat == "yyyy.dd.mm" || dateFormat == "yyyy/dd/mm" || dateFormat == "yyyy-dd-mm") {
                              var m1 = mn;
                              var d1 = dt;
                              var y1 = yr;
                              mn = y1;
                              yr = d1;
                              dt = m1;
                            }
                        
                            if (parseInt(yr) >= parseInt(minYear) && parseInt(yr) <= parseInt(maxYear)) {
                              // do nothing
                            } else {
                              invalid = "true";
                            }
                            if (mn.length > 2) {
                              invalid = "true";
                            }
                            if (dt.length > 2) {
                              invalid = "true";
                            }
                        
                            if (t[0] == "") {
                              invalid = "true";
                            }
                            if (t[1] == "") {
                              invalid = "true";
                            }
                            if (t[2] == "") {
                              invalid = "true";
                            }
                        
                            if (j != 3) {
                              invalid = "true";
                            }
                            var dc;
                            var daysArray = "0-31-28-31-30-31-30-31-31-30-31-30-31";
                        
                            if ((yr % 4) == 0) {
                              daysArray = "0-31-29-31-30-31-30-31-31-30-31-30-31";
                            }
                            var days = daysArray.split("-");
                            if (parseInt(dt) >= 1 && parseInt(dt) <= days[parseInt(mn)]) {} else {
                              invalid = "true";
                            }
                        
                            if (yr.length != 4) {
                              invalid = "true";
                            }
                        
                            var i;
                            var m;
                        
                            for (i = 0; i < ln; i++) {
                              m = f.charAt(i);
                              if (m == '0' || m == '1' || m == '2' || m == '3' || m == '4' || m == '5' || m == '6' || m == '7' || m == '8' || m == '9' || m == delm) {} else {
                                invalid = "true";
                              }
                            }
                          } else {
                            invalid = "true";
                          }
                        
                          if (invalid == "true") {
                            alert("Invalid Date");
                          } else {
                            alert("valid Date");
                          }
                        
                        }

                        【讨论】:

                        • 这是一个漫长的解决方案。当您将大写字母转换为小写字母而不是使用许多String.prototype.replace 方法时,您可以使用一个String.prototype.toLowerCase。您还可以使用/、- 和. 作为日、月和年之间的分隔符。您可以使用分隔符生成这些格式,而不是硬编码。
                        • 这是一个冗长的代码,但如果您想验证日期,它几乎可以在所有日期格式中实现并处于工作状态
                        【解决方案24】:
                        var date = new Date(date_string)
                        

                        为任何无效的 date_string 返回文字 'Invalid Date'。

                        注意:请看下面的评论。

                        【讨论】:

                        猜你喜欢
                        • 2016-01-26
                        • 2010-09-21
                        • 2017-04-07
                        • 2014-01-18
                        • 2022-11-29
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多