System.DateTime.Now和System.DateTime.Today之间的区别

Sam*_*iew 124 .net c# datetime date

任何人都可以解释C#.NET 之间System.DateTime.Now和之间的区别System.DateTime.Today吗?如果可能的话,各自的利弊.

Mat*_*int 170

DateTime.Now返回一个DateTime值,该值由运行代码的计算机的本地日期和时间组成.它已DateTimeKind.Local分配给它的Kind财产.它相当于调用以下任何一项:

  • DateTime.UtcNow.ToLocalTime()
  • DateTimeOffset.UtcNow.LocalDateTime
  • DateTimeOffset.Now.LocalDateTime
  • TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Local)
  • TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.Local)

DateTime.Today返回DateTime具有与上述任何表达式相同的年,月和日组件的值,但时间组件设置为零.它也有DateTimeKind.Local它的Kind财产.它相当于以下任何一项:

  • 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时间(通过GetSystemTimeAsFileTimeWin32 API中的函数),然后将值转换为本地时区.(因此DateTime.Now.ToUniversalTime()比它贵DateTime.UtcNow.)

另请注意,DateTimeOffset.Now.DateTime它将具有相似的值DateTime.Now,但它将具有DateTimeKind.Unspecified而不是DateTimeKind.Local- 这可能会导致其他错误,具体取决于您使用它做什么.

所以,简单的答案DateTime.Today是等同于DateTime.Now.Date.
但恕我直言 - 你不应该使用其中任何一个,或任何上述等价物.

当您要求时DateTime.Now,您要求运行代码的计算机的本地日历时钟的值.但你得到的东西没有关于那个时钟的任何信息!你得到的最好的是DateTime.Now.Kind == DateTimeKind.Local.但是谁的本地呢?只要您对值执行任何操作,该信息就会丢失,例如将其存储在数据库中,在屏幕上显示或使用Web服务传输.

如果您的本地时区遵循任何夏令时规则,则不会从中获取该信息DateTime.Now.在模糊的时间,例如在"后退"过渡期间,您将不知道两个可能的时刻中的哪一个与您检索到的值相对应DateTime.Now.例如,假设您的系统时区设置为2013年11月3日凌晨,Mountain Time (US & Canada)您要求的是DateTime.Now什么.结果2013-11-03 01:00:00意味着什么?由同一日历日期时间表示的瞬时时间有两个时刻.如果我把这个价值发给别人,他们就不知道我的意思.特别是如果他们处于规则不同的时区.

你可以做的最好的事情是使用DateTimeOffset:

// This will always be unambiguous.
DateTimeOffset now = DateTimeOffset.Now;
Run Code Online (Sandbox Code Playgroud)

现在对于我上面描述的相同场景,我2013-11-03 01:00:00 -0600在转换之前或2013-11-03 01:00:00 -0700转换之后得到了值.任何看过这些价值观的人都可以说出我的意思.

我写了一篇关于这个主题的博客文章.请阅读 - 针对DateTime.Now的案例.

此外,在这个世界上有一些地方(如巴西),"春天前进"过渡恰好发生在午夜.时钟从23:59到01:00.这意味着您DateTime.Today在该日期获得的价值不存在! 即使您使用DateTimeOffset.Now.Date,也会得到相同的结果,但您仍然遇到此问题.这是因为传统上,Date.Net中没有对象这样的东西.因此无论你如何获得价值,一旦你剥离了时间 - 你必须记住它并不真正代表"午夜",即使这是你正在使用的价值.

如果您真的想要一个完全正确的解决方案来解决这个问题,最好的方法是使用NodaTime.该LocalDate课程恰当地代表没有时间的日期.您可以获取任何时区的当前日期,包括本地系统时区:

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;
Run Code Online (Sandbox Code Playgroud)

如果您不想使用Noda Time,现在有另一种选择.我已经为.Net CoreFX Lab项目贡献了一个仅限日期对象的实现.您可以System.Time在MyGet Feed中找到包对象.添加到项目后,您会发现可以执行以下任何操作:

using System;
...

Date localDate = Date.Today;

Date utcDate = Date.UtcToday;

Date tzSpecificDate = Date.TodayInTimeZone(anyTimeZoneInfoObject);
Run Code Online (Sandbox Code Playgroud)

  • 使用`DateTime.UtcNow`代替`DateTimeOffset.Now`怎么样? (8认同)
  • 如果您可以在您的应用程序或规范中传达该值为UTC,则可以接受`DateTime.UtcNow`.(我喜欢实际上将字段或属性称为"MyDateUtc"而不仅仅是"MyDate" - 但这只是锦上添花.)如果你不能在规范或字段名称中传达它,那么`DateTimeOffset.UtcNow `可用于确保零偏移量与日期和时间值一起传达. (5认同)

Mar*_*ell 84

时间..Now包括09:23:12或其他什么; .Today是日期部分(当天00:00:00).

因此,.Now如果您想要包含时间,请使用,.Today如果您只想要日期!

.Today 与...基本相同 .Now.Date

  • ...并使用`UtcNow`,除非你*真的*想要系统本地时区.(特别是在一个几乎总是错误选择的网络应用程序上.) (27认同)

Guf*_*ffa 22

例如,该DateTime.Now属性返回当前日期和时间2011-07-01 10:09.45310.

例如,该DateTime.Today属性返回当前日期,时间组件设置为零2011-07-01 00:00.00000.

DateTime.Today属性实际上是为了返回DateTime.Now.Date:

public static DateTime Today {
  get {
    DateTime now = DateTime.Now;
    return now.Date;
  }
}
Run Code Online (Sandbox Code Playgroud)


dan*_*ken 9

DateTime.Today表示当前系统日期,时间部分设置为00:00:00

DateTime.Now表示当前系统日期和时间

  • @megaperlz:您现在链接到4.0而不是vLatest.可以通过删除`(v = VS.100)`来实现最大的链接. (3认同)
  • 只是一个观察... 1.1文档远没有4.0文档那么详细; 链接到vLatest可能更好吗? (2认同)

dek*_*dev 6

我想到了添加这些链接 -

回到原始问题,使用Reflector我解释了代码的差异

 public static DateTime Today
    {
      get
      {
        return DateTime.Now.Date;   // It returns the date part of Now

        //Date Property
       // returns same date as this instance, and the time value set to 12:00:00 midnight (00:00:00) 
      }
    }


    private const long TicksPerMillisecond = 10000L;
    private const long TicksPerDay = 864000000000L;
    private const int MillisPerDay = 86400000;

    public DateTime Date
    {
       get
      {
        long internalTicks = this.InternalTicks; // Date this instance is converted to Ticks 
        return new DateTime((ulong) (internalTicks - internalTicks % 864000000000L) | this.InternalKind);  
// Modulo of TicksPerDay is subtracted - which brings the time to Midnight time 
      }
    }


     public static DateTime Now
        {
          get
          {
           /* this is why I guess Jon Skeet is recommending to use  UtcNow as you can see in one of the above comment*/
            DateTime utcNow = DateTime.UtcNow;


            /* After this i guess it is Timezone conversion */
            bool isAmbiguousLocalDst = false;
            long ticks1 = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utcNow, out isAmbiguousLocalDst).Ticks;
            long ticks2 = utcNow.Ticks + ticks1;
            if (ticks2 > 3155378975999999999L)
              return new DateTime(3155378975999999999L, DateTimeKind.Local);
            if (ticks2 < 0L)
              return new DateTime(0L, DateTimeKind.Local);
            else
              return new DateTime(ticks2, DateTimeKind.Local, isAmbiguousLocalDst);
          }
        }
Run Code Online (Sandbox Code Playgroud)


dee*_*epi 5

DateTime dt = new DateTime();// gives 01/01/0001 12:00:00 AM
DateTime dt = DateTime.Now;// gives today date with current time
DateTime dt = DateTime.Today;// gives today date and 12:00:00 AM time
Run Code Online (Sandbox Code Playgroud)