IgorStack
IgorStack

Reputation: 869

Compute difference of DateTimes from different time zones

There are 2 events (start and stop of some job) in the past which happened in different time zones.

I need to compute duration of the job. I can get difference between those time zones in minutes and compute duration like this:

var duration = endTime - startTime.AddMinutes(diff);

However, there is a thing which confuses me.
Suppose start event happend in California and end event was in say Israel.
Right now difference between these time zones is 9 hours - California just switched to the day light and Israel not yet.
Next week this difference will be 10 hours because Israel will switch.

So, duration will be different if computed now and on the next week.

What is the right way to compute it?
Use DateTimeOffset?

Upvotes: 0

Views: 688

Answers (1)

nitsram
nitsram

Reputation: 716

So, duration will be different if computed now and on the next week.

I think you're forgetting that its you who is moving through time. not the project start and end times. therefore results will always be the same.

Id be tempted though just to convert the second time into the zone of the first, so it's just a straight diff. to convert from utc to any other zone you can use the following code

DateTime timeUtc = DateTime.UtcNow;

TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);

Theres also good info at https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones

Upvotes: 1

Related Questions