4

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.

Dizzle
  • 1,026
  • 13
  • 26
  • checkout @JonSkeet blog http://codeblog.jonskeet.uk/category/nodatime/ – MethodMan Oct 30 '15 at 13:25
  • 1
    Can you please clarify your question? If you wanna checks their time zone properties, you can use `Zone` property of instances which returns `DateTimeZone`. – Soner Gönül Oct 30 '15 at 13:29
  • 1
    Do you want the elapsed time between the two underlying instants? Or do you want the difference in wall-clock times? – Matt Johnson-Pint Oct 30 '15 at 14:02
  • An example of what you're looking for would be very useful here :) – Jon Skeet Oct 30 '15 at 14:27
  • @MattJohnson I believe I would like to work in the wall clock time. Please see my edit above for more information. To JonSkeet also please see my edit above hopefully it explains further. – Dizzle Oct 30 '15 at 15:16
  • Your revised question says *"...number of milliseconds between the two periods respecting DST..."* which would imply elapsed time, but then you asked for wall-clock time.... Please be clear. Perhaps tell us the intended use case? Provide some code maybe? Note, the server time is irrelevant. – Matt Johnson-Pint Oct 30 '15 at 15:35
  • Also, read [my answer to a similar question](http://stackoverflow.com/a/32832524/634824). – Matt Johnson-Pint Oct 30 '15 at 15:39
  • "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... – Jon Skeet Oct 30 '15 at 16:16

2 Answers2

7

Try this:

ZonedDateTime t1 = LocalDateTime.FromDateTime(startTime).InUtc();
ZonedDateTime t2 = LocalDateTime.FromDateTime(endTime).InUtc();
Duration diff = t2.ToInstant() - t1.ToInstant();
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    That's starting with two `DateTime` values, which isn't what the OP is interested in... – Jon Skeet Oct 30 '15 at 14:27
  • 1
    Well, we can assume the OP already has `t1` and `t2`, and the last line is then correct with regard to getting the elapsed duration between them, though we're still not sure if that's what the OP is asking for or not. – Matt Johnson-Pint Oct 30 '15 at 15:39
  • this is what i was hoping for I just didnt understand the problem domain well enough when I asked the question – Dizzle Nov 30 '16 at 15:20
0

"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.

Dizzle
  • 1,026
  • 13
  • 26
  • If you're going to self-answer, it would at least help if you'd update the question to be more specific. Then I suspect a better answer than this could be written, too... – Jon Skeet Nov 01 '15 at 12:49