DateTime parsing

2019-06-14 21:10发布

I am writing a syslog server that receives syslog messages and stores them in a database.

I am trying to parse the date string received in the message into a DateTime structure.

For the following examples, I'll be using an underscore in place of whitespace for clarity; the actual strings received have spaces.

The string I received is in the format "Jun__7_08:09:10" - please note the two whitespaces between the month and day.

If the day is after the 10th, the strings become "Jun_10_08:09:10" (one whitespace).

If I parse with:

DateTime.ParseExact(Log.Date, "MMM  d HH:mm:ss", CultureInfo.InvariantCulture);

it works for strings from the 1st to 9th but throws exception from the 10th forward, and if I parse with one space, it throws an exception on the 1st to 9th (and works from the 10th on).

What is the correct way to parse this string?

5条回答
ら.Afraid
2楼-- · 2019-06-14 21:36

Use the DateTime.ParseExact overload that takes an array of format strings:

DateTime.ParseExact(Log.Date, 
                    new [] {"MMM  d HH:mm:ss", "MMM d HH:mm:ss"}, 
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None);
查看更多
Juvenile、少年°
3楼-- · 2019-06-14 21:37

DateTime's ParseExect Method has some overloads where you can pass multiple format that could be readed if the earlier one is not working. here a sample for you..

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                         "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                         "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                         "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                         "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
      string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", 
                              "5/1/2009 6:32:00", "05/01/2009 06:32", 
                              "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}; 
      DateTime dateValue;

      foreach (string dateString in dateStrings)
      {
         try {
            dateValue = DateTime.ParseExact(dateString, formats, 
                                            new CultureInfo("en-US"), 
                                            DateTimeStyles.None);
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
         catch (FormatException) {
            Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
         }                                               
      }
   }
}
// The example displays the following output:
//       Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
//       Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
//       Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
//       Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
查看更多
Evening l夕情丶
4楼-- · 2019-06-14 21:43

Consider using this line:

DateTime.ParseExact(Log.Date,
    "MMM d HH:mm:ss",
    CultureInfo.InvariantCulture,
    DateTimeStyles.AllowWhiteSpaces);

Notice that I removed one of the spaces between the month and the day. That's because AllowWhiteSpaces literally means:

Specifies that s may contain leading, inner, and trailing white spaces not defined by format.

查看更多
Explosion°爆炸
5楼-- · 2019-06-14 21:46
    DateTime.ParseExact(date, "MMM d HH:mm:ss", CultureInfo.InvariantCulture,DateTimeStyles.AllowInnerWhite)
查看更多
ら.Afraid
6楼-- · 2019-06-14 21:57

You could remove the extra space first and then parse the string:

DateTime.ParseExact(Log.Date.Replace("  ", " "), "MMM d HH:mm:ss", CultureInfo.InvariantCulture);
查看更多
登录 后发表回答