I am trying to convert times to different time zones, but not the way you're thinking. I need to convert a DateTime that is 9am EST to 9am CST on the UTC for example. The timezones are variable so just adding/subtracting hours doesn't seem correct way to do it with NodaTime
Fri, 21 Feb 2014 21:00:00 EST = 1393034400 Epoch Timestamp
convert to
Fri, 21 Feb 2014 21:00:00 CST = 1393030800 Epoch Timestamp
On Msdn website you can find all you need.
Small example:
Go to the link for more details on what you want, I can't give you more with that small description
If I understand the question correctly, it sounds like you're trying to convert a date/time in one time zone to another one that has the same local time and a different time zone; that is, a different point in time.
You can do this with Noda Time by combining the
LocalDateTime
with the new zone. For example, given something like the following:nowEastern
is the time now in theAmerica/New_York
time zone. If we printnowEastern
directly to the console, we'll see something like2014-02-22T05:18:50 America/New_York (-05)
.As an aside, "EST" and "CST" aren't time zones: they're non-unique abbreviations for a particular offset within a time zone;
America/New_York
andAmerica/Chicago
are probably representative of what we think of as "Eastern" and "Central", though (or you could use something likeUTC-05:00
if you really wanted EST even when daylight savings time was in effect).Given a
ZonedDateTime
in any time zone, we can convert it to aZonedDateTime
with the same local time and a specified time zone as follows:This gives us a
ZonedDateTime
with the same local time, but a different time zone. With the input above, the result would be2014-02-22T05:18:50 America/Chicago (-06)
.Note that I'm using
InZoneStrictly
. This will throw an exception if the local time is ambiguous or invalid (for example, during daylight savings transitions). If that's unacceptable, you could useInZoneLeniently
, which picks the earliest validZonedDateTime
on or after the given local time, orInZone
, which allows you to specify your own rules in those cases.