通过


DateTime.Date 属性

定义

获取此实例的日期组件。

public:
 property DateTime Date { DateTime get(); };
public DateTime Date { get; }
member this.Date : DateTime
Public ReadOnly Property Date As DateTime

属性值

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

示例

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

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
open System

let date1 = DateTime(2008, 6, 1, 7, 47, 0)
printfn $"{date1}"

// Get date-only portion of date, without its time.
let dateOnly = date1.Date
// Display date using short date string.
printfn $"{dateOnly:d}"
// Display date using 24-hour clock.
printfn $"{dateOnly:g}"
printfn $"""{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
Module Example
   Public Sub Main()
      Dim date1 As Date = #6/1/2008 7:47AM#
      Console.WriteLine(date1.ToString())
      
      ' Get date-only portion of date, without its time.
      Dim dateOnly As Date = 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"))   
   End Sub
End Module
' The example displays output like the following:
'       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 日期错误解释为日期和时间。

适用于