1

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?

4

2 回答 2

3

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
}
于 2012-11-14T07:31:10.403 回答
0

下面的正则表达式代码片段可能有助于验证日期和时间。这也照顾闰年。

^(?=\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}) ?$
于 2012-11-14T07:56:47.183 回答