Possible Duplicate:
Regular expression to validate valid time
i wrote this code for validating time in asp.net *
what are the errors if i use "[0-9]?[0-9]+:[0-9]+[0-9]+:[0-9]+[0-9]+" for hh:mm:ss format.Will it work for all cases?
Possible Duplicate:
Regular expression to validate valid time
i wrote this code for validating time in asp.net *
what are the errors if i use "[0-9]?[0-9]+:[0-9]+[0-9]+:[0-9]+[0-9]+" for hh:mm:ss format.Will it work for all cases?
You may use TimeSpan.TryParse rather than regex to parse a string and see if you get true
or false
TimeSpan tempTimeSpan;
if (TimeSpan.TryParse("12:22:33", out tempTimeSpan))
{
//valid time
}
else
{
//Invalid time
}
下面的正则表达式代码片段可能有助于验证日期和时间。这也照顾闰年。
^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(? =.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][ 26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]| 1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2 -9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0- 5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}) ?$