Regular/JavaScript/日期

来自康健生活
Kangkk​(留言 | 贡献)2020年2月23日 (日) 08:17的版本 (创建页面,内容为“==== 日期的有效范围 ==== [https://msdn.microsoft.com/ MSDN]中定义的[https://msdn.microsoft.com/zh-cn/library/system.datetime(v=vs.110).aspx DateTime]对…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

日期的有效范围

MSDN中定义的DateTime对象的有效范围是:

The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.

UNIX时间戳的0按照ISO 8601规范为 : 1970-01-01T00:00:00Z。

闰年

闰年(leap year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。

日期的格式

根据不同的语言文化,日期的连字符会有所不同,通常有以下几种格式:

yyyyMMdd yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd

方案一

  • 1、3、5、7、8、10、12月为1-31日
  • 平年2月为1-28日,闰年2月为1-29日
  • 4、6、9、11月为1-30日

正则实现(方案一)

暂定实现格式:yyyy-MM-dd

一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月
1-31 平年 1-28 1-31 1-30 1-31 1-30 1-31 1-31 1-30 1-31 1-30 1-31
闰年 1-29
年份
[0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}
月份
(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])
月份为 1, 3, 5, 7, 8, 10, 12,天数范围为 01-31
(0[469]|11)-(0[1-9]|[12][0-9]|30)
月份为 4, 6, 9, 11,天数范围为 01-30
02-(0[1-9]|[1][0-9]|2[0-8])
月份为 2(平年)
除闰年的2月外的其它所有日期
([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))
闰年
闰年年份 闰年规则 闰年正则
for(var i=1990;i<2019;i++){
    if((i%4==0&&i%100!=0)||i%400==0){
        console.log(i);
    }
}
1992 1996 2000 2004 2008 2012 2016
  1. 个位数必是偶数
  2. 十位数为奇数时,个位数只有两种可能:2和6
  3. 十位数为偶数(除0外)时,个位数有三种可能:0、4、8
  4. 十位数为0时,个位数为4和8
四年一闰,百年不闰
([0-9]{2})(0[48]|[2468][048]|[13579][26])
四百年再闰
(0[48]|[2468][048]|[3579][26])00
所有闰年的2月29日
(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29
最终符合DateTime范围的日期的正则
(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)

DD/MM/YYYY格式

(((0[1-9]|[12][0-9]|3[01])/((0[13578]|1[02]))|((0[1-9]|[12][0-9]|30)/(0[469]|11))|(0[1-9]|[1][0-9]|2[0-8])/(02))/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(29/02/(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00)))

方案二

  • 所有年份的所有月份都包含1-28日
  • 所有年份除2月外都包含29和30日
  • 所有年份1、3、5、7、8、10、12月都包含31日
  • 闰年2月包含29日
一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月
29-30
31
平年 1-28 29-30
31
29-30 29-30
31
29-30 29-30
31
29-30
31
29-30 29-30
31
29-30 29-30
31
闰年 29

正则实现(方案二)

暂定实现格式:yyyy-MM-dd

年份
(?!0000)[0-9]{4}
月份
(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])
包括平年在内的所有年份的月份都包含1-28日
(0[13-9]|1[0-2])-(29|30)
包括平年在内的所有年份除2月外都包含29和30日
(0[13578]|1[02])-31)
包括平年在内的所有年份1、3、5、7、8、10、12月都包含31日
除闰年的2月29日外的其它所有日期
(?!0000)[0-9]{4}-((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)
闰年
闰年年份 闰年规则 闰年正则
for(var i=1990;i<2019;i++){
    if((i%4==0&&i%100!=0)||i%400==0){
        console.log(i);
    }
}
1992 1996 2000 2004 2008 2012 2016
  1. 个位数必是偶数
  2. 十位数为奇数时,个位数只有两种可能:2和6
  3. 十位数为偶数(除0外)时,个位数有三种可能:0、4、8
  4. 十位数为0时,个位数为4和8
四年一闰
([0-9]{2}(0[48]|[2468][048]|[13579][26])
百年不闰,四百年再闰
(0[48]|[2468][048]|[13579][26])00
所有闰年的2月29日
([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29
最终符合DateTime范围的日期的正则
^((?!0000)[0-9]{4}-((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)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$

模板:Regular JavaScript 日期 方案二 非捕获组优化

使用非捕获组优化的正则
^(?:(?!0000)[0-9]{4}-(?:(?: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)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$

验证正则的有效性和性能

var regex = new RegExp("(?:(?!0000)[0-9]{4,4}\\-(?:(?: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)|(?:[0-9]{2,2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)\\-02\\-29)", "g");
var testString = ""; // fill this in
var match;
while ((match = regex.exec(testString)) != null) {
  // javascript RegExp has a bug when the match has length 0
  if (match.index === regex.lastIndex) {
    ++regex.lastIndex;
  }
  // the match variable is an array that contains the matching groups
}
性能测试
//
//
//
//
//
//
//
//
//
//
//
//

方案三

  • 匹配除了2月份之外的1-30日
  • 若1无法匹配,则匹配1,3,5,7,8,10,12月份的31日
  • 若2无法匹配,则匹配2月份的1-28日
  • 若以上都无法匹配,那只可能剩下一天,那就是闰年2月份的最后一天2月29日

正则实现(方案三)

暂定实现格式:yyyy-MM-dd

月份 日期
31天的月份
(0?[13578]|1[02])
(0?[1-9]|[12]\d|3[01])
30天的月份
(0?[469]|11)
(0?[1-9]|[12]\d|30)
2月28天
0?2
(0?[1-9]|1\d|2[0-8])
最终符合1900-2099范围的日期的正则
((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$

日期正则表达式扩展

扩展格式连字符
yyyy-M-d,yyyy.M.d
^(?:(?!0000)[0-9]{4}([-/.]?)(?:(?: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)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2([-/.]?)29)$
反向引用进行简化
年份0001-9999
格式yyyy-MM-dd或yyyy-M-d
连字符可以没有或是“-”、“/”、“.”
^(?:(?!0000)[0-9]{4}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))$
其它格式
dd/MM/yyyy
^(?:(?:(?:0?[1-9]|1[0-9]|2[0-8])([-/.]?)(?:0?[1-9]|1[0-2])|(?:29|30)([-/.]?)(?:0?[13-9]|1[0-2])|31([-/.]?)(?:0?[13578]|1[02]))([-/.]?)(?!0000)[0-9]{4}|29([-/.]?)0?2([-/.]?)(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00))$
添加时间的扩展
时间的规格
HH:mm:ss和H:m:s
([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]
合并
yyyy-MM-dd HH:mm:ss
^(?:(?!0000)[0-9]{4}-(?:(?: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)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\s+([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$

时间格式判定

连词符(-,/,.)
\-|\/|\.
yyyy
[0-9]{4}
\d{4}
MM
[0-9]{2}
0\d{1}|1[0-2]
[0][1-9]|[1][0-2]
dd
[0-9]{2}
0\d{1}|[12]\d{1}|3[01]
HH
[0-9]{2}
0\d{1}|1\d{1}|2[0-3]
[0-1][0-9]|[2][0-3]
[0-9]|0[0-9]|1[0-9]|2[0-3]
mm
[0-9]{2}
[0-5]\d{1}
[0-5][0-9]
ss
[0-9]{2}
[0-5]\d{1}
SS
yyyy/MM/dd
^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$
HH:mm:ss(12h)
(0[0-9]|1[01]):([0-5][0-9]):([0-5][0-9])
HH:mm:ss(24h)
[0-2][0-3]:[0-5][0-9]:[0-5][0-9]
# 匹配(00:00 | 23:59 | 10:10)不匹配(24:00 | 00:60 | 25:61)
(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])

其他