I try to parse date from string which contains time zone information. Input string is 2014-12-17T08:05:39+00:00
.
I use DateTime.Parse()
method which return me 2014-12-17 09:05:39
(one hour was added). I live in UTC+1:00 (Warsaw)
, so .NET adopt this date to my local time.
My question is how to use the parse method while skipping time zone, for example for 2014-12-17T08:05:39+00:00
I want to get 2014-12-17 08:05:39
.
I would recommend parsing it as a DateTimeOffset
instead of as a DateTime
. You can then get the DateTime
out of that, but it separates the "parsing the data you've been given" step from the "only using the bits I want from that" step.
It's possible that there are ways to make DateTime.Parse
behave the way you want using DateTimeStyles
- and I'm surprised it's converting to a "local" kind automatically anyway - but using DateTimeOffset
will make it clearer.
(Of course I'd really recommend using Noda Time instead, parsing to an OffsetDateTime
and then getting the LocalDateTime
out of that, but that's a different matter...)
See more on this question at Stackoverflow