DateTime.Date 属性

定义

获取此实例的日期部分。

C#
public DateTime Date { get; }

属性值

一个新对象,其日期与此实例相同,时间值设置为午夜 12:00:00 (00:00:00)。

示例

以下示例使用 Date 属性提取值的日期部分 DateTime ,其时间部分设置为零 (或 0:00:00,或午夜) 。 它还说明,根据显示 DateTime 值时使用的格式字符串,时间分量可以继续显示在格式化输出中。

C#
using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
      Console.WriteLine(date1.ToString());

      // Get date-only portion of date, without its time.
      DateTime dateOnly = date1.Date;
      // Display date using short date string.
      Console.WriteLine(dateOnly.ToString("d"));
      // Display date using 24-hour clock.
      Console.WriteLine(dateOnly.ToString("g"));
      Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
   }
}
// The example displays output like the following output:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00

注解

返回DateTime值的 Kind 属性的值与当前实例的值相同。

由于 类型 DateTime 表示单个类型中的日期和时间,因此请务必避免将 属性返回 Date 的日期错误解释为日期和时间。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0