I have an ISO 8601 timestamp in the format:
YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm]
YYYY-MM-DDThh:mm:ss[{+|-}hh:mm]
Examples:
2013-07-03T02:16:03.000+01:00
2013-07-03T02:16:03+01:00
How can I parse it to a .NET Framework DateTime
with correct TimeZone
supplied?
The DateTime.TryParse
doesn't work because the trailing info regarding the TimeZone
.
You should be able to format it using
DateTimeOffset
and theK
custom format specifier. You can then convert that to aDateTime
afterwards if you want to. Sample code:One thing to note is that this is badly named - it's not actually a time zone, it's just a UTC offset. It doesn't actually tell you the original time zone. (There can be several different time zones observing the same offset at the same time.)
Or with Noda Time (unstable version, which will become 1.2 pretty soon):