c# - Parsing ISO 8601 with timezone to .NET datetime -
i have iso 8601 timestamp in 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 parse .net framework datetime
correct timezone
supplied?
the datetime.tryparse
doesn't work because trailing info regarding timezone
.
you should able format using datetimeoffset
, k
custom format specifier. can convert datetime
afterwards if want to. sample code:
using system; using system.globalization; class test { static void main() { string text = "2013-07-03t02:16:03.000+01:00"; string pattern = "yyyy-mm-dd't'hh:mm:ss.fffk"; datetimeoffset dto = datetimeoffset.parseexact (text, pattern, cultureinfo.invariantculture); console.writeline(dto); } }
one thing note badly named - it's not time zone, it's utc offset. doesn't tell original time zone. (there can several different time zones observing same offset @ same time.)
or noda time (unstable version, become 1.2 pretty soon):
string text = "2013-07-03t02:16:03.000+01:00"; offsetdatetimepattern pattern = offsetdatetimepattern.extendedisopattern; offsetdatetime odt = pattern.parse(text).value; console.writeline(odt);
Comments
Post a Comment