小时两次之间相隔,无论国家和时区的 [英] Hours elapsed between two times, irrespective of country and time zones

查看:220
本文介绍了小时两次之间相隔,无论国家和时区的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何才能确定两个时间之间所经过的时间。例如:

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

下午3:30在旧金山和7:30 PM迪拜

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

这是我的一部分目前还不清楚的是,是否有计算减去时间跨度,通过采取时区和国家交代的普遍方式。

The part that I am unclear is that, is there a universal way of calculating the subtracted timespan, by taking time zones and countries in to account.

我使用C#作为主要语言。任何帮助将十分赞赏。

I am using C# as a primary language. Any help will be deeply appreciated.

先谢谢了。

推荐答案

你问到:

3:30 PM在旧金山和7:30 PM迪拜

如果你知道的时间和地点,那么你有一个问题,因为并不是所有时区都是固定的实体。迪拜固定为UTC + 4,但UTC-8之间旧金山候补太平洋标准时间和UTC-7是太平洋夏令时。因此,一个的日期的还需要进行这种转换。

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

下面将使用给你差的电流的日期在第一时间区:

The following will give you 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);

(注意:ID 太平洋标准时间是美国太平洋时区的Windows时区标识符,尽管名称,涵盖PST和PDT)

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

不过,即使这不一定是最好的方法,因为今天不是所有时区相同。在(几乎)在任何时间点,也有在操作中两个不同的天全世界。此图维基百科证明这一点。

But even this is not necessarily the best approach because "today" isn't the same in all time zones. At (almost) any point in time, there are two different days in operation worldwide. This illustration from Wikipedia demonstrates this well.

                                                       

                                                       

这是可能的当前的日期为源时区的的到底时区目前相同的日期。如果没有额外的投入,有没有什么可以做这一点。你需要知道每个值问题的日期。

It's possible that the "current" date for the source time zone is not the same date currently in the end time zone. Without additional input, there's not much you can do about that. You need to know the date in question for each value.

这篇关于小时两次之间相隔,无论国家和时区的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆