简体   繁体   中英

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.

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

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