简体   繁体   中英

Parsing custom date formats (c#)

An api im using is providing me a date. This date is of type string and is presented in the format:

Mon Nov 16 19:15:09 +0000 2009

DateTime.TryParse() fails when this value is provided. Can anyone point me in the right direction?

Using the DateTimeOffset class in order to handle the offset.

[TestMethod]
public void test()
{
  string s = "Mon Nov 16 19:15:09 +0000 2009";

  var result = DateTimeOffset.ParseExact(
    s, 
    "ddd MMM dd HH:mm:ss zzz yyyy", 
    System.Globalization.CultureInfo.InvariantCulture);

  Assert.AreEqual(16, result.Day);
  Assert.AreEqual(11, result.Month);
  Assert.AreEqual(2009, result.Year);
  Assert.AreEqual(19, result.Hour);
  Assert.AreEqual(15, result.Minute);
  Assert.AreEqual(9, result.Second);
  Assert.AreEqual(0, result.Offset.Hours);    
}

Change the offset in the string - eg '0600' and then change the offset assertion to match, it'll work.

You can then convert this into a DateTime if you have to - but you lose the offset information; so you have to decide whether you're going to keep it as the original local time ( 19:15:09 ), or if you're going to convert to some standard time (eg 13:19:05 UTC if offset is +06:00 ).

It gets interesting if you need to convert that to your own local time - because it would depend on what DST rules were in place in 2009 at that time of the year - that can cause a real headache!

So, if you're going to DateTime I recommend converting to universal time and then go from there. Add this to the test:

Console.WriteLine(result);
//little bit long winded - but you need the 'Universal' Kind for reliability
Console.WriteLine(
  DateTime.SpecifyKind(
    new DateTime(result.ToUniversalTime().Ticks), 
    DateTimeKind.Utc)
);    

This outputs:

11/16/2009 19:15:09 +06:00

11/16/2009 13:15:09

尝试DateTime.TryParseExact传递适当的格式字符串。

One of the TryParse methods accepts an IFormatProvider, which can also come as a DateTimeFormatInfo class. The following link has all the necessary details:

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx

Yours would be almost like: ddd, MMM dd yyyy HH':'mm':'ss zzz yyyy

The only problem is the timezone offset, zzz includes a colon between the hours and minutes. You might get away with using zz'00' though it is cheating.

Because the DateTime.TryParse(String, DateTime) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use the DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime) method or one of the overloads of the TryParseExact method and provide a format specifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM