System.DateTime.Now和System.DateTime.Today的区别 [英] Difference between System.DateTime.Now and System.DateTime.Today
问题描述
谁能解释之间的差异 System.DateTime.Now
和 System.DateTime.Today
在C#.NET ?如果每一个可能的优点和缺点。
Can anyone explain the difference between System.DateTime.Now
and System.DateTime.Today
in C#.NET? Pros and cons of each if possible.
推荐答案
DateTime.Now
返回的DateTime
价值它由其中code正在运行的计算机的本地日期和时间。它有 DateTimeKind.Local
分配到类
属性。它等同于调用任何以下的:
DateTime.Now
returns a DateTime
value that consists of the local date and time of the computer where the code is running. It has DateTimeKind.Local
assigned to its Kind
property. It is equivalent to calling any of the following:
-
DateTime.UtcNow.ToLocalTime()
-
DateTimeOffset.UtcNow.LocalDateTime
-
DateTimeOffset.Now.LocalDateTime
-
TimeZoneInfo.ConvertTime(DateTime.UtcNow,TimeZoneInfo.Local)
-
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,TimeZoneInfo.Local)
DateTime.UtcNow.ToLocalTime()
DateTimeOffset.UtcNow.LocalDateTime
DateTimeOffset.Now.LocalDateTime
TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Local)
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.Local)
DateTime.Today
返回的DateTime
值具有相同的年,月,日组成部分为已任上述前pressions,但随着时间的成分设置为零。它还具有 DateTimeKind.Local
在类
属性。它等同于任何以下的:
DateTime.Today
returns a DateTime
value that has the same year, month, and day components as any of the above expressions, but with the time components set to zero. It also has DateTimeKind.Local
in its Kind
property. It is equivalent to any of the following:
-
DateTime.Now.Date
-
DateTime.UtcNow.ToLocalTime()。日期
-
DateTimeOffset.UtcNow.LocalDateTime.Date
-
DateTimeOffset.Now.LocalDateTime.Date
-
TimeZoneInfo.ConvertTime(DateTime.UtcNow,TimeZoneInfo.Local).Date
-
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,TimeZoneInfo.Local).Date
DateTime.Now.Date
DateTime.UtcNow.ToLocalTime().Date
DateTimeOffset.UtcNow.LocalDateTime.Date
DateTimeOffset.Now.LocalDateTime.Date
TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Local).Date
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.Local).Date
请注意在内部,系统时钟是UTC的角度,所以当你调用 DateTime.Now
它首先获取UTC时间(通过<一href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724397.aspx"><$c$c>GetSystemTimeAsFileTime$c$c>功能在Win32 API),然后将其值设置为本地时区转换。 (因此 DateTime.Now.ToUniversalTime()
比 DateTime.UtcNow
更贵。)
Note that internally, the system clock is in terms of UTC, so when you call DateTime.Now
it first gets the UTC time (via the GetSystemTimeAsFileTime
function in the Win32 API) and then it converts the value to the local time zone. (Therefore DateTime.Now.ToUniversalTime()
is more expensive than DateTime.UtcNow
.)
另外请注意, DateTimeOffset.Now.DateTime
将有类似的值 DateTime.Now
,但它会产生 DateTimeKind.Unspecified
,而不是 DateTimeKind.Local
- 这可能会导致这取决于你用它做其他错误
Also note that DateTimeOffset.Now.DateTime
will have similar values to DateTime.Now
, but it will have DateTimeKind.Unspecified
rather than DateTimeKind.Local
- which could lead to other errors depending on what you do with it.
所以,简单的答案是, DateTime.Today
等同于 DateTime.Now.Date
。
但是恕我直言 - 你不应该使用其中一种的这些或任何上述当量
So, the simple answer is that DateTime.Today
is equivalent to DateTime.Now.Date
.
But IMHO - You shouldn't use either one of these, or any of the above equivalents.
当你请求 DateTime.Now
,你所要求的是,code上运行的计算机的本地日历时钟的值。但是,你回来没有关于该时钟的任何信息!你得到的最好的是, DateTime.Now.Kind == DateTimeKind.Local
。但其局部是什么呢?这些信息一旦丢失了你的价值做任何事情,比如它存储在数据库中,显示在屏幕上,或者使用Web服务发送。
When you ask for DateTime.Now
, you are asking for the value of the local calendar clock of the computer that the code is running on. But what you get back does not have any information about that clock! The best that you get is that DateTime.Now.Kind == DateTimeKind.Local
. But whose local is it? That information gets lost as soon as you do anything with the value, such as store it in a database, display it on screen, or transmit it using a web service.
如果您的本地时区将遵循任何夏时制规则,你没有得到这些信息从 DateTime.Now
返回。在暧昧的时候,例如在一个回退的过渡,你不会知道哪两个可能的时刻对应于您检索到 DateTime.Now
的值。例如,假设您的系统时区设置为山地时间(美国和加拿大)
,你问 DateTime.Now
凌晨11月3日的,2013年是什么结果 2013年11月3号01:00:00
是什么意思?有瞬时间再$ P $的两个时刻由同一日历日期时间psented。如果我要发送这个值给别人,他们不知道我的意思是哪一个。特别是如果他们是在一个时区,其中规则不同。
If your local time zone follows any daylight savings rules, you do not get that information back from DateTime.Now
. In ambiguous times, such as during a "fall-back" transition, you won't know which of the two possible moments correspond to the value you retrieved with DateTime.Now
. For example, say your system time zone is set to Mountain Time (US & Canada)
and you ask for DateTime.Now
in the early hours of November 3rd, 2013. What does the result 2013-11-03 01:00:00
mean? There are two moments of instantaneous time represented by this same calendar datetime. If I were to send this value to someone else, they would have no idea which one I meant. Especially if they are in a time zone where the rules are different.
您可以做的最好的事情是使用的DateTimeOffset
来代替:
The best thing you could do would be to use DateTimeOffset
instead:
// This will always be unambiguous.
DateTimeOffset now = DateTimeOffset.Now;
现在的我上面描述相同的情况下,我得到的值 2013年11月3日1点〇〇分00秒-0600
在过渡之前,或 2013年11月3号一点00分00秒-0700
之后的过渡。任何人都希望在这些数值可以告诉我是什么意思。
Now for the same scenario I described above, I get the value 2013-11-03 01:00:00 -0600
before the transition, or 2013-11-03 01:00:00 -0700
after the transition. Anyone looking at these values can tell what I meant.
我写了关于这个主题的博客文章。请阅读 - 的案,DateTime.Now
I wrote a blog post on this very subject. Please read - The Case Against DateTime.Now.
另外,还有一些在这个世界上某些地方(如巴西),其中春进转变正是发生在午夜。该时钟去从23:59至01:00。这意味着你的 DateTime.Today
在该日期值,不存在!的即使你使用的DateTimeOffset .Now.Date
,你得到同样的结果,你仍然有这个问题。这是因为没有这样的东西日期
对象.NET。因此,无论你如何获得的价值,一旦你剥离的时间 - 你要记住,它并没有真正重新present子夜,即使那是你正在使用的值
Also, there are some places in this world (such as Brazil) where the "spring-forward" transition happens exactly at Midnight. The clocks go from 23:59 to 01:00. This means that the value you get for DateTime.Today
on that date, does not exist! Even if you use DateTimeOffset.Now.Date
, you are getting the same result, and you still have this problem. It is because there is no such thing as a Date
object in .Net. So regardless of how you obtain the value, once you strip off the time - you have to remember that it doesn't really represent "midnight", even though that's the value you're working with.
如果你真的想要一个完全正确的解决这个问题,最好的方法是使用 NodaTime 。该 LocalDate
类正确地重新presents没有时间约会。您可以获取当前日期为任何时区,包括本地系统时区:
If you really want a fully correct solution to this problem, the best approach is to use NodaTime. The LocalDate
class properly represents a date without a time. You can get the current date for any time zone, including the local system time zone:
using NodaTime;
...
Instant now = SystemClock.Instance.Now;
DateTimeZone zone1 = DateTimeZoneProviders.Tzdb.GetSystemDefault();
LocalDate todayInTheSystemZone = now.InZone(zone1).Date;
DateTimeZone zone2 = DateTimeZoneProviders.Tzdb["America/New_York"];
LocalDate todayInTheOtherZone = now.InZone(zone2).Date;
这篇关于System.DateTime.Now和System.DateTime.Today的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!