The โincorrectโ result (as the answer to the dark suit explains) is due to the fact that on March 11, 2012, 2:38:58 a.m., there was no valid date and time in areas of the United States and Canada that observe daylight saving time. Obviously, your code runs on a computer in one of these areas.
To convert a string to DateTime , ignoring any timezone offset, you can call DateTimeOffset.Parse and then get the DateTime component of the result:
DateTime dt = DateTimeOffset.Parse("2012-03-11T02:53:58-08:00").DateTime; string strDt = dt.ToString();
UPDATE:. What is the difference between DateTime.Parse and DateTimeOffset.Parse when the source string contains a timezone offset? Consider these two examples, which assume your current time zone is Pacific Time:
// Example 1: DateTime.Parse(String) DateTime dt = DateTime.Parse("2012-03-11T06:00:00-04:00"); Console.WriteLine(dt.ToString("o")); // 2012-03-11T03:00:00.0000000-07:00
DateTime.Parse uses an offset to edit the syntax date and time local . Please note that the time has changed from 6:00 to 3:00, which reflects the transition from Eastern daylight (UTC-04: 00) to Pacific daylight (UTC-07: 00). In your question, the time has changed because DateTime.Parse automatically adjusted the time from Pacific Standard Time (UTC-08: 00) to Quiet Daylight Time (UTC-07: 00).
// Example 2: DateTimeOffset.Parse(String) DateTimeOffset dto = DateTimeOffset.Parse("2012-03-11T06:00:00-04:00"); Console.WriteLine(dto.DateTime.ToString("o")); // 2012-03-11T06:00:00.0000000 Console.WriteLine(dto.Offset); // -04:00:00
DateTimeOffset.Parse easier. It simply returns a DateTimeOffset value whose DateTime and Offset properties are set to parse date, time, and offset. But be careful: If the time zone offset in the line does not match the time zone you want to work with, then you need to set the date and time yourself.
source share