两次之间的时间,不分国家和时区 [英] Hours elapsed between two times, irrespective of country and time zones

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

问题描述

我们如何确定两次之间经过的时间。例如:

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

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

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和迪拜下午7:30

如果你知道的是时间和位置,那么你有一个问题,因为并不是所有的时区都是固定的实体。迪拜固定在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 太平洋标准时间 code>是美国太平洋时区的Windows时区标识符,尽管名称涵盖了PST和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天全站免登陆