Reputation: 1016
Using nodatime how do I find the difference between two ZonedDateTime objects specifically in the ZonedDateTime timezones.
Edit - Example:
For example I have two dates in a timezone lets use "Europe/Stockholm" time.
These are being calculated on a server which has its local time set to "America/Los_Angeles".
I wish to get the number of milliseconds between the two periods respecting DST in the "Europe/Stockholm" timezone whilst ignoring the local time of the server. This is because the server local time can possibly change if deployed to a different server and I don't wish to update the code if that happens.
Upvotes: 4
Views: 2413
Reputation: 1016
"Between two periods" doesn't make much sense, as a ZonedDateTime isn't a period. If you have two ZonedDateTime values, you probably just need to call ToInstant on both of them, then subtract one instant from the other to get the duration..."
This comment from Jon Skeet answers the question, Calling ToInstant allows you to use the subtract [-] and add [+] operators.
Upvotes: 0
Reputation: 172638
Try this:
ZonedDateTime t1 = LocalDateTime.FromDateTime(startTime).InUtc();
ZonedDateTime t2 = LocalDateTime.FromDateTime(endTime).InUtc();
Duration diff = t2.ToInstant() - t1.ToInstant();
Upvotes: 6