How to check if a string is date?

前端 未结 5 2104
渐次进展
渐次进展 2020-12-17 19:39

I am having string like

\"11-04-2015 22:01:13:053\" or \"32476347656435\"

How can I check if string is Date?
Checking String if it is n

5条回答
  •  长情又很酷
    2020-12-17 19:49

    The best solution is to actually try to parse it to a date with DateTime.TryParse().

    String d = "11-04-2015 22:01:13:053";
    
    DateTime dt = new DateTime();
    
    if (DateTime.TryParse(d, out dt)) { 
        /// yes, it's a date, do something here... 
    } else {
        // no, it's not a date, do something else here... 
    }
    

提交回复
热议问题