How to convert ZonedDateTime to local DateTime?

6,918 views
Skip to first unread message

Gleb Chermennov

unread,
Apr 3, 2013, 1:25:29 PM4/3/13
to noda...@googlegroups.com
I have a ZonedDateTime instance and I'd like to convert it to local DateTime in particular timezone - I know timeZoneId for that one.
Question is - how do I do that? I was ready to do smth like zonedDateTime.ToDateTimeUtc().AddHours(offsetInHours), where offsetInHours represent the difference in hours between that particular timezone and UTC.
And then I thought - "wait, I want not just any offset, I want offset from a particular point in time (taking into account DST)". And now I'm stuck. Does NodaTime know about what was the offset at given point in time at given timezone? Going through the docs, I didn't find the method.

Jon Skeet

unread,
Apr 3, 2013, 1:33:47 PM4/3/13
to Noda Time
On 3 April 2013 18:25, Gleb Chermennov <thebitt...@gmail.com> wrote:
I have a ZonedDateTime instance and I'd like to convert it to local DateTime in particular timezone - I know timeZoneId for that one.

But it's a different time zone to the one that you've already got? You can use WithZone to convert to the target time zone.

Once you've got a ZonedDateTime representing the right instant in time and the right time zone, you can just use the LocalDateTime property.
 
Question is - how do I do that? I was ready to do smth like zonedDateTime.ToDateTimeUtc().AddHours(offsetInHours), where offsetInHours represent the difference in hours between that particular timezone and UTC.

You definitely don't need to go through all of those hoops :)
 
And then I thought - "wait, I want not just any offset, I want offset from a particular point in time (taking into account DST)". And now I'm stuck. Does NodaTime know about what was the offset at given point in time at given timezone?

Yes.
 
Going through the docs, I didn't find the method.

It sounds like you don't actually need it here, but you can get the offset from a ZonedDateTime using the Offset property. 

Hope that answers everything - but if not, feel free to clarify. Hopefully we've got you covered :)

Jon

Gleb Chermennov

unread,
Apr 3, 2013, 1:47:33 PM4/3/13
to noda...@googlegroups.com
Yep, target timezone is different from the original one. So, I just do zonedDateTime.WithZone(targetZone).ToDateTimeUtc().AddHours(targetZone.Offset.Hours) and the code will know the right offset between target zone and UTC?

среда, 3 апреля 2013 г., 21:33:47 UTC+4 пользователь Jon Skeet написал:

Jon Skeet

unread,
Apr 3, 2013, 1:53:54 PM4/3/13
to Noda Time
No, you just use:

Offset offset = zonedDateTime.WithZone(targetZone).Offset;

Or if you want to convert a ZonedDateTime to just a BCL DateTime, use

zonedDateTime.WithZone(targetZone).ToDateTimeUnspecified()

There's no need to start adding the offset onto anything else yourself. Note that if you did use ToDateTimeUtc().AddHours(...) you'd end up with an incorrect value, because the DateTime would have a Kind of UTC, even though it represented a local time.

Jon



--
 
---
You received this message because you are subscribed to the Google Groups "Noda Time" group.
To unsubscribe from this group and stop receiving emails from it, send an email to noda-time+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gleb Chermennov

unread,
Apr 3, 2013, 1:56:52 PM4/3/13
to noda...@googlegroups.com
pardon me, I meant to write: var localDateTimeInTargetTimeZone = zonedDateTime.WithZone(targetZone).ToDateTimeUtc().AddHours(targetZone.GetOffsetFromUtc(zonedDateTime.ToInstant()).Hours);

среда, 3 апреля 2013 г., 21:47:33 UTC+4 пользователь Gleb Chermennov написал:

Gleb Chermennov

unread,
Apr 3, 2013, 1:57:53 PM4/3/13
to noda...@googlegroups.com
Oh, I see. Thanks for the fast response.

среда, 3 апреля 2013 г., 21:53:54 UTC+4 пользователь Jon Skeet написал:

abhi...@gmail.com

unread,
Oct 28, 2013, 6:18:17 AM10/28/13
to noda...@googlegroups.com
How convert milliseconds to zoned date time?

Jon Skeet

unread,
Oct 28, 2013, 6:29:16 AM10/28/13
to Noda Time
Assuming you mean "milliseconds since the epoch":

var zoned = Instant.FromMillisecondsSinceUnixEpoch(millis).InZone(zone);
Reply all
Reply to author
Forward
0 new messages