Regular expression to validate valid time

后端 未结 8 1681
余生分开走
余生分开走 2020-12-03 14:24

Can someone help me build a regular expression to validate time?

Valid values would be from 0:00 to 23:59.

When the time is less than 10:00 it should also su

8条回答
  •  星月不相逢
    2020-12-03 14:43

    I don't want to steal anyone's hard work but this is exactly what you're looking for, apparently.

    using System.Text.RegularExpressions;
    
    public bool IsValidTime(string thetime)
    {
        Regex checktime =
            new Regex(@"^(20|21|22|23|[01]d|d)(([:][0-5]d){1,2})$");
    
        return checktime.IsMatch(thetime);
    }
    

提交回复
热议问题