如何在考虑时区的情况下比较DateTime值? [英] How to compare DateTime values, taking into account timezone?

查看:102
本文介绍了如何在考虑时区的情况下比较DateTime值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 DateTime 变量。每个变量都有一个时区,因此当我 ToString 的格式包括zzz时,我得到的字符串包括 +01:00

I have two DateTime variables. Each has a timezone stored in the variable so that when I ToString with format including zzz I get a string including +01:00.

在设计时,我不知道时区是什么,并且我期望变量具有彼此不同的时区。

At design time I do not know what the timezones will be and I am expecting the variables to have different timezones from each other.

我想比较两个 DateTime 值,以便我知道哪个是最新的。

I want to compare the two DateTime values so that I know which is more recent.

例如,如果变量A为 2015-07-04T02:00:00 + 03:00 而变量B为 2015-07 -03T18:00:00-07:00 然后是B> A。

For example, if variable A is 2015-07-04T02:00:00+03:00 and variable B is 2015-07-03T18:00:00-07:00 then B > A.

我用C#写什么告诉我这个? (我宁愿不使用第三方库。)

What do I write in C# to tell me this? (I would prefer not to use a third party library.)

(对于SO结束者,我已经花了几个小时来使用Google,MSDN和SO进行调查并感到困惑。我在SO上找不到与此类似的问题。我有信心这个问题的答案将对其他人有所帮助。)

(To the SO question-closing zealots: I have spent several hours investigating this using Google, MSDN and SO and am confused. I cannot find a very similar question to this on SO. I am confident that answers to this question will help others.)

推荐答案

您说:


我有两个DateTime变量。每个变量都有一个时区,因此当我使用包含zzz格式的ToString时,我会得到一个包含+01:00的字符串。

I have two DateTime variables. Each has a timezone stored in the variable so that when I ToString with format including zzz I get a string including +01:00.

这是一个普遍的误解。 DateTime 没有在变量中存储时区。它仅具有 Kind 属性,该属性的类型为 DateTimeKind ,并且可以为 Utc 本地未指定

This is a common misunderstanding. DateTime doesn't have a time zone stored in the variable. It only has a Kind property, which is of type DateTimeKind, and can be either Utc, Local, or Unspecified.

调用 ToString 时, zzz 格式说明符使用 Kind 属性来确定要显示的偏移量。

When calling ToString, the zzz format specifier uses the Kind property to determine which offset to display.


  • 种类 DateTimeKind.Utc ,偏移量始终是 +00:00

种类 DateTimeKind.Local 时,偏移量从执行代码的计算机上的本地时区。例如,我的计算机设置为美国太平洋时间,因此偏移量将为 -08:00 -07:00 ,具体取决于夏令时是否有效。

When the Kind is DateTimeKind.Local, the offset is determined from the local time zone on the computer where the code is executing. For example, my computer is set to US Pacific time, so the offset will be either -08:00 or -07:00 depending on whether daylight saving time is in effect or not.

种类生效时 DateTimeKind.Unspecified ,其行为与 Local 相同。请记住,其他方法以不同的方式对待未指定-这只是 zzz 说明符的特殊行为。

When the Kind is DateTimeKind.Unspecified, the behavior is the same as if it were Local. Keep in mind that other methods treat Unspecified in different ways - this is just the particular behavior of the zzz specifier.

MSDN实际上说


因此, zzz格式说明符不建议将其与 DateTime 值一起使用。

回到您的问题:


在设计时,我不知道时区是什么,并且我期望变量具有彼此不同的时区。 / p>

At design time I do not know what the timezones will be and I am expecting the variables to have different timezones from each other.

然后,您将无法使用 DateTime 。您应该改用 DateTimeOffset ,因为它保留了特定的时区偏移量,而不是使用 DateTimeKind

Then you cannot use DateTime. You should instead use DateTimeOffset, as it retains a specific time zone offset instead of using a DateTimeKind.


例如,如果变量A为2015-07-04T02:00:00 + 03:00且变量B为2015-07-03T18:00: 00-07:00然后B>A。我该怎么用C#写这个告诉我?

For example, if variable A is 2015-07-04T02:00:00+03:00 and variable B is 2015-07-03T18:00:00-07:00 then B > A. What do I write in C# to tell me this?



DateTimeOffset a = DateTimeOffset.Parse("2015-07-04T02:00:00+03:00");
DateTimeOffset b = DateTimeOffset.Parse("2015-07-03T18:00:00-07:00");

bool result = b > a;  // true

另请参见:日期时间与日期时间偏移

此外

正如古斯塔夫指出的那样,只要您转换回通用格式,您就可以仅使用 DateTime 比较之前的时间。这是由于 DateTime 的隐藏的第四状态更多此处)。状态在解析过程中已正确设置,并在调用 ToUniversalTime 时将其考虑在内。然后比较将具有有效的UTC时间。

As Gustav pointed out, you can use just DateTime, as long as you convert back to universal time before comparing. This works due to DateTime's hidden fourth state (more here). The state is set properly during parsing, and is taken into account when ToUniversalTime is called. Then comparison has valid UTC times to operate from.

DateTime A = DateTime.Parse("2015-11-01T01:00:00-07:00");
DateTime B = DateTime.Parse("2015-11-01T01:00:00-08:00");

Console.WriteLine(A.ToUniversalTime().ToString("'A: 'yyyy'-'MM'-'dd hh:mm:ss"));
Console.WriteLine(B.ToUniversalTime().ToString("'B: 'yyyy'-'MM'-'dd hh:mm:ss"));
Console.WriteLine( B.ToUniversalTime() > A.ToUniversalTime() );
Console.WriteLine( B > A );

结果为:

A: 2015-11-01 08:00:00
B: 2015-11-01 09:00:00
True
False

如果您的本地时区设置为太平洋时间,则会得到上述结果。但是,如果将其设置为其他值,则可能会得到 True 的最后一个结果,因为这些值可能已解析为不同的 local 即使您的时区与太平洋时区的当地时间相同相同。

If your local time zone is set to Pacific Time, you'll get the above results. However, if it's set to something else - it's possible you will get True for the last result, because the values may have been parsed to different local times in your time zone, even though they'd be the same local time in the Pacific time zone.

使用 DateTimeOffset 仍然更简单,转换次数更少,并且不受本地时区的影响。

Using DateTimeOffset is still simpler, going through less conversions, and not being affected by the local time zone.

这篇关于如何在考虑时区的情况下比较DateTime值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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