String contains only a given set of characters

前端 未结 7 1463
执笔经年
执笔经年 2021-02-18 13:22

I need to know if a given string is a valid DateTime format string because the string may represent other things. I tried DateTime.ParseExact(somedate.ToString(format), format)

7条回答
  •  天涯浪人
    2021-02-18 13:50

    Something like

    Regex regex = new Regex("^(y|Y|m|M|d|D|s|S|h|H)+$");
    if (regex.IsMatch('DateTime String'))
    {
        // 'valid' 
    }
    

    if you're literally searching for those characters and not the numerical representation for a given date and time

提交回复
热议问题