2

I'm working on parsing few XML files where I have datetime values saved as text. I'm not able to find what format is the below one -

20110123T233356,00-05

I tried both DateTime.Parse and DateTimeOffset.Parse and both of them failed. I also tried to identify the string in few places like here and here with no luck.

pravprab
  • 2,301
  • 3
  • 26
  • 43
NLV
  • 21,141
  • 40
  • 118
  • 183

1 Answers1

8

Assuming that the ",00" is hundredths of seconds and the "-05" is the timezone, you can parse it like this:

string dateStr = "20110123T233356,00-05";
string format = @"yyyyMMdd\THHmmss\,ffzz";

DateTime result;

if (DateTime.TryParseExact(dateStr, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
    Console.WriteLine(result);
}
else
{
    Console.WriteLine("Can't parse the date: " + dateStr);
}

However, that's a big assumption.

Note that you can also specify the format string without escaping the T or the , as follows (but I escaped them to make it more obvious that they aren't format characters):

string format = "yyyyMMddTHHmmss,ffzz";
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Not a "big" assumption (it's by far the most likely case), but an assumption nonetheless. – Rik Mar 14 '14 at 10:01
  • Works perfectly. For now I will make that big assumption. Thank you. – NLV Mar 14 '14 at 10:01
  • Correct me if I'm wrong, but you don't need the slashes in the format string. – Rik Mar 14 '14 at 10:02
  • 1
    @Rik That's correct - I mention this at the end of my answer. I just like to make non-format characters more obvious by escaping them with slashes. – Matthew Watson Mar 14 '14 at 10:03