我需要用正则表达式验证格式为dd/mm/yyyy的日期字符串。
这个正则表达式验证dd/mm/yyyy,但不验证像31/02/4500这样的无效日期:
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
什么是有效的正则表达式来验证支持闰年的dd/mm/yyyy格式?
我需要用正则表达式验证格式为dd/mm/yyyy的日期字符串。
这个正则表达式验证dd/mm/yyyy,但不验证像31/02/4500这样的无效日期:
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
什么是有效的正则表达式来验证支持闰年的dd/mm/yyyy格式?
当前回答
下面是regex的另一个版本,它可以匹配以下任何日期格式,并允许省略前导零:
Regex: ^(0 - 3) ?[0 - 9][0 - 3] ?[0 - 9]。(?:[0 - 9]{2})?[0 - 9] {2} $
匹配:
1/1/11或1.1.11或1-1-11:true 01/01/11或01.01.11或01-01-11:true 01/01/2011或011.01.2011或01-01-2011:true 01/1/2011或01.1.2011或01-1-2011:true 1/11/2011或1.11.2011或1-11-2011:true 1/11/11或1.11.11或1-11-11:true 11/1/11或11.1.11或11-1-11:true
Debuggex演示
其他回答
我知道这是一个切题的答案,但如果问题的意图是“我如何验证日期?”,那么为什么不试着让编程语言来完成所有的艰苦工作(如果你正在使用一种语言的话)?
例如在PHP中
$this_date_object = date_create($this_date);
if ($this_date_object == false )
{
// process the error
}
另一种答案,根据月(mm)和年(yyyy)验证日(dd)(即,在闰年也验证2月29日),并允许年范围从0001到9999(根据公历,0000是无效年)
^(?:(?:(?:0[1-9]|[12]\d|3[01])/(?:0[13578]|1[02])|(?:0[1-9]|[12]\d|30)/(?:0[469]|11)|(?:0[1-9]|1\d|2[0-8])/02)/(?!0000)\d{4}|(?:(?:0[1-9]|[12]\d)/02/(?:(?!0000)(?:[02468][048]|[13579][26])00|(?!..00)\d{2}(?:[02468][048]|[13579][26]))))$
我怀疑,在不知道用户的地区何时从儒略历切换到格里高利历的情况下,以下内容是尽可能准确的。
它接受'-','/',或者什么都不作为年,月和日之间的分隔符,不管顺序如何。
MMddyyyy:
^(((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))[-/]?[0-9]{4}|02[-/]?29[-/]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$
ddMMyyyy:
^(((0[1-9]|[12][0-9]|30)[-/]?(0[13-9]|1[012])|31[-/]?(0[13578]|1[02])|(0[1-9]|1[0-9]|2[0-8])[-/]?02)[-/]?[0-9]{4}|29[-/]?02[-/]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$
yyyyMMdd:
^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$
除了顺序,这些都精确到儒略历(每四年闰年),直到1700年,当公历与儒略历背离。它有两个问题:
It accepts the year 0000, which doesn't exist in many, but not all, standards. Note that ISO 8601 does accept year 0000 (equivalent to 1 BCE). It doesn't skip the 10-13 days which were lost when the Gregorian Calendar came into use. This varies by locality though. For example, the Roman Catholic Church skipped 10 days, October 5th through October 14th, 1582, but Greece (the last to switch) skipped February 16th through the 28th of 1923, 13 days, having to take into account the leap years of 1700, 1800, and 1900.
从0001年到9999年的Java日历实现已经测试了这一点,唯一的差异是上面提到的1582年的10天。
如果您正在寻找特定的格式,这适用于“dd/MM/yyyy”和“dd/MMM/yyyy”日期格式,仅基于Alok答案。
function isValidDateFormatDDMMYYYY(inputDate) {
var date_regex = /^(?:(?:31(\/)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
return date_regex.test(inputDate);
}
一些示例通过这段代码-
isValidDateFormatDDMMYYYY("15/01/1999") //返回True isValidDateFormatDDMMYYYY("15/Jan/1999") //返回True isValidDateFormatDDMMYYYY("15/1/1999") //返回True isValidDateFormatDDMMYYYY("1/15/1999") //返回False
谢谢
下面的表达式很好,也很容易操作:
((((0[13578]|1[02])(\/|-|.)(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)(\/|-|.)(0[1-9]|1[0-9]|2[0-9]|3[0]))|((02)((\/|-|.)(0[1-9]|1[0-9]|2[0-8]))))(\/|-|.)(19([6-9][0-9])|20(0[0-9]|1[0-4])))|((02)(\/|-|.)(29)(\/|-|.)(19(6[048]|7[26]|8[048]|9[26])|20(0[048]|1[26])))
它根据MM/dd/YYYY格式进行验证,并允许从1960年到2016年的闰年支持。如果你需要扩展闰年支持,你只需要操作表达式的这一部分:
(19(6[048]|7[26]|8[048]|9[26])|20(0[048]|1[26]))
希望这对你有帮助