DateTime.Date 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得這個實例的日期成分。
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 屬性擷取時間分量為零(或 0:00:00,午夜)的值的日期成分 DateTime 。 同時也說明,根據顯示值 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 類型同時代表同一類型中的日期與時間,因此避免將物業回傳 Date 的日期誤認為日期與時間非常重要。