Javascript 通过 jQuery 验证日期字符串格式的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11218181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:57:58  来源:igfitidea点击:

Best way to validate date string format via jQuery

javascriptjqueryjquery-validate

提问by Developer

I found a lot of links to validate string if it is a date.

我发现了很多链接来验证字符串是否是日期。

Like hereand here.

喜欢这里这里

But anyway I cannot figure out how to validate if we have this thing:

但无论如何我无法弄清楚如何验证我们是否有这个东西:

6/6/2012where first 6is month and the second 6is days

2012 年66日,其中第一个6是月份,第二个 6是天

and also if user input it like that:

并且如果用户这样输入:

06/06/2012

06/06/2012

Any clue how it could be done in a proper way?

任何线索如何以正确的方式完成?

Thanks!!

谢谢!!

回答by elclanrs

Here, this should work with any date format with 4 digit year and any delimiter. I extracted it from my plugin Ideal Formswhich validates dates and much more.

在这里,这应该适用于任何带有 4 位年份和任何分隔符的日期格式。我从我的插件Ideal Forms 中提取了它,它可以验证日期等等。

var isValidDate = function (value, userFormat) {
  var

  userFormat = userFormat || 'mm/dd/yyyy', // default format

  delimiter = /[^mdy]/.exec(userFormat)[0],
  theFormat = userFormat.split(delimiter),
  theDate = value.split(delimiter),

  isDate = function (date, format) {
    var m, d, y
    for (var i = 0, len = format.length; i < len; i++) {
      if (/m/.test(format[i])) m = date[i]
      if (/d/.test(format[i])) d = date[i]
      if (/y/.test(format[i])) y = date[i]
    }
    return (
      m > 0 && m < 13 &&
      y && y.length === 4 &&
      d > 0 && d <= (new Date(y, m, 0)).getDate()
    )
  }

  return isDate(theDate, theFormat)

}

回答by merv

Use a regular expression.

使用正则表达式。

var dateRegEx = /^(0[1-9]|1[012]|[1-9])[- /.](0[1-9]|[12][0-9]|3[01]|[1-9])[- /.](19|20)\d\d$/

console.log("06/06/2012".match(dateRegEx) !== null) // true
console.log("6/6/2012".match(dateRegEx) !== null) // true
console.log("6/30/2012".match(dateRegEx) !== null) // true
console.log("30/06/2012".match(dateRegEx) !== null) // false

Learn about RegEx.

了解正则表达式。

Edit - A Disclaimer

编辑 - 免责声明

As @elclanrs pointed out, this only validates the string's format, and not the actual date, which means that dates like February 31st would pass. However, since the OP only asks "to validate date string format," I'll keep this answer here because for some, it might be all you need.

正如@elclanrs 指出的那样,这只验证字符串的format,而不是实际日期,这意味着像 2 月 31 日这样的日期会过去。但是,由于 OP 只要求“验证日期字符串格式”,因此我将这个答案保留在这里,因为对于某些人来说,这可能就是您所需要的。

As a note, the jQuery Validation pluginthat the OP was using, also only validates format.

请注意,OP 使用的jQuery 验证插件也仅验证格式。

Finally, for those wondering, if you needed to validate the date, and not just the format, this regular expression would have ~2% rate of failureover the domain of (1-12)/(1-31)/(1900-2099)date strings. Please don't use this in Mission Critical code for JPL.

最后,对于那些想知道,如果你需要验证的日期,而不仅仅是格式,此正则表达式将有故障的〜2%的速度在域(1-12)/(1-31)/(1900- 2099)日期字符串。请不要在 JPL 的关键任务代码中使用它。