Search code examples
c#datetimedatatable

Exception String was not recognized as a valid DateTime


I get this error message:

String was not recognized as a valid DateTime. stack: at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)

I think this is just happening when I try to convert to DateTime, a time string that starts with 0:   I was looking at this link: parseExact not recognized as valid datetime

I have a script that reads from my log files, and I need to see if the date is in my range I care about. I don't want to check with a regular expression every line to see if the date correctly has two 00's in the start since there are too many. How do I get it to convert to DateTime so it behaves correctly? We don't want to have to edit the log or logging.

string serverPath = @"\\\\" + serverName;
string logNameFull = logName + ".txt";
string partialPath = @"\build\logs\";
string combinedPath2 = partialPath + logNameFull;
Uri serverUri = new Uri(serverPath + combinedPath2);
string finalPath = serverUri.LocalPath;
DataTable dataTable = ReadDataTable(finalPath);

foreach (DataRow row in dataTable.Rows)
{
    try
    {
        if (Regex.IsMatch((row[startTimeHeader]).ToString(), @"^\d+")) //date starts with number..filter out rows that have general logging instead of dated table entries
        {
            string tmpStartDT = (row[startTimeHeader]).ToString();
            startDate = DateTime.ParseExact(tmpStartDT, "MM/dd/yyyy HH:mm:ss", provider); //problem line
        }
   }
   catch (Exception ex)
   {
      Console.WriteLine("Caught exception for  row: {0} CName:{1} on line close to:{2} server:{3} logname:{4} startDate:{5}", row.ToString(), cName, debug_i, serverName, logName, row[startTimeHeader]);
      Console.WriteLine("Message: " + ex.Message + " stack:" + ex.StackTrace);
   }
}  // foreach

It prints out:

Caught exception for row: System.Data.DataRow CName:CopyLogFile on line close to:475 server:mach62 logname:ParseLogStats startDate:04/14/2024 0:38:07

The line in the log is this:

04/14/2024  0:38:07 04/14/2024  0:38:13 Sun 00:00:06    6   Success v24.1 Incr Stage_M  0   967 0   0   0   0   0   0   0   I:\logs\Stage_.log  
Message: String was not recognized as a valid DateTime. stack:   at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
   at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
   at BuildStatusVerification.BuildStatusVerification.ParseLogFile(String codeFreezeTime, String startTimeHeader, String serverName, String logName, String cName, String cValue, String field1, String comparison1, String val) in C:\BuildStatusVerification\BuildStatusVerification\BuildStatusVerification.cs:line 150

Solution

  • Use H:mm:ss as the format specifier instead of HH:mm:ss so it'll accept single-digit hours. Trim tmpStartDT to remove trailing spaces. tmpStartDT = tmpStartDT.Replace(" ", " "); to change double spaces to single spaces.