Finding if a string contains a date and time

前端 未结 5 1168
渐次进展
渐次进展 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条回答
  •  Happy的楠姐
    2020-12-16 01:25

    Update 2: Found out using DateTime.TryParseExact is a much better way that using Regex Expressions

    DateTime myDate;
    if (DateTime.TryParseExact(inputString, "dd-MM-yyyy hh:mm:ss", 
        CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate))
    {
        //String has Date and Time
    }
    else
    {
        //String has only Date Portion    
    }
    

提交回复
热议问题