NLV
NLV

Reputation: 21657

String to DateTime Parsing

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.

Upvotes: 2

Views: 189

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109792

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";

Upvotes: 8

Related Questions