Finding if a string contains a date and time

前端 未结 5 1163
渐次进展
渐次进展 2020-12-16 00:57

I am working on a project where I am reading in a file which could come in two different formats, one includes a date and time and the other doesn\'t.

When I read i

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 01:33

    This worked for us:

    If the string value is a valid datetime value, then it will not give any exception:

    try
    {
        Convert.ToDateTime(string_value).ToString("MM/dd/yyyy");
    }
    

    If the string value is an invalid datetime value, then it will give exception:

    catch (Exception)
    {
    }
    

提交回复
热议问题