Time elapsed between two moments, regardless of country and time zones

how can we determine the hours elapsed between two times. For instance:

3:30 pm in San Francisco and 7:30 pm in Dubai

Part of what I am unclear is that there is a universal way to calculate the deductible time, by taking into account time zones and countries for accounting.

I use C # as my main language. Any help would be greatly appreciated.

Thanks in advance.

+5
source share
3 answers

You asked about:

"3:30 pm in San Francisco and 7:30 pm in Dubai"

If all you know is time and location, then you have a problem because not all time zones are fixed objects. Dubai is fixed at UTC + 4, but San Francisco alternates between UTC-8 for Pacific Standard Time and UTC-7 for Pacific Daylight Time. Therefore, a date is required for this conversion.

The following is the difference using the current date in the first time zone:

TimeZoneInfo tz1 = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); TimeZoneInfo tz2 = TimeZoneInfo.FindSystemTimeZoneById("Arabian Standard Time"); DateTime today = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz1).Date; DateTime dt1 = new DateTime(today.Year, today.Month, today.Day, 15, 30, 0); // 3:30 PM DateTime dt2 = new DateTime(today.Year, today.Month, today.Day, 19, 30, 0); // 7:30 PM TimeSpan elapsed = TimeZoneInfo.ConvertTimeToUtc(dt1, tz1) - TimeZoneInfo.ConvertTimeToUtc(dt2, tz2); 

(Note that the "Pacific Standard Time" identifier is the Windows time zone identifier for the US Pacific and, despite the name, covers both PST and PDT.)

But even this is not necessarily the best approach, because today is not the same in all time zones. At (almost) any point in time, two different days are active throughout the world. This Wikipedia illustration demonstrates this well.

Date animation

It is possible that the β€œcurrent” date for the time zone of the source does not match the current date in the end time zone. Without additional input, you are unlikely to be able to do this. You must know the date in question for each value.

+4
source

If you use the DateTimeOffset type, you can avoid some errors related to the use of local time, since it can represent local time in any time zone:

 var dateTimeOffsetSanFrancisco = DateTimeOffset.Parse("9/8/2014 3:30PM -0700"); var dateTimeOffsetDubai = DateTimeOffset.Parse("9/8/2014 7:30PM +0400"); var elapsedHours = (dateTimeOffsetDubai - dateTimeOffsetSanFrancisco).TotalHours; 
+2
source

The easiest way is to convert both DateTime objects to UTC, and then subtract them:

 var utcSanFran = sfLocal.ToUniversalTime(); var utcDubai = dubaiLocal.ToUniversalTime(); var elapsed = dubaiLocal - sfLocal; 

Assuming Dubai> San Francisco.

To get the total hours used:

 elapsed.TotalHours; 

Note that this apparently only works in the time zone of the local computer. See http://msdn.microsoft.com/en-us/library/system.timezoneinfo(v=vs.110).aspx for a better way to do this.

0
source

Source: https://habr.com/ru/post/1202046/


All Articles