DateTime.Today 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
拿到目前的日期。
public:
static property DateTime Today { DateTime get(); };
public static DateTime Today { get; }
static member Today : DateTime
Public Shared ReadOnly Property Today As DateTime
屬性值
一個設定為今天日期的物件,時間成分設為 00:00:00。
範例
以下範例使用該 Date 屬性來取得當前日期。 同時也說明了如何利用一些標準的日期與時間格式字串來格式化值 DateTime 。 請注意,第三次呼叫該 ToString(String) 方法所產生的輸出使用了 g 格式指定符,包含時間成分,時間成分為零。
using System;
public class Example
{
public static void Main()
{
// Get the current date.
DateTime thisDay = DateTime.Today;
// Display the date in the default (general) format.
Console.WriteLine(thisDay.ToString());
Console.WriteLine();
// Display the date in a variety of formats.
Console.WriteLine(thisDay.ToString("d"));
Console.WriteLine(thisDay.ToString("D"));
Console.WriteLine(thisDay.ToString("g"));
}
}
// The example displays output similar to the following:
// 5/3/2012 12:00:00 AM
//
// 5/3/2012
// Thursday, May 03, 2012
// 5/3/2012 12:00 AM
open System
// Get the current date.
let thisDay = DateTime.Today
// Display the date in the default (general) format.
printfn $"{thisDay}\n"
// Display the date in a variety of formats.
printfn $"{thisDay:d}"
printfn $"{thisDay:D}"
printfn $"{thisDay:g}"
// The example displays output similar to the following:
// 5/3/2012 12:00:00 AM
//
// 5/3/2012
// Thursday, May 03, 2012
// 5/3/2012 12:00 AM
Module modMain
Public Sub Main()
' Get the current date.
Dim thisDay As DateTime = DateTime.Today
' Display the date in the default (general) format.
Console.WriteLine(thisDay.ToString())
Console.WriteLine()
' Display the date in a variety of formats.
Console.WriteLine(thisDay.ToString("d"))
Console.WriteLine(thisDay.ToString("D"))
Console.WriteLine(thisDay.ToString("g"))
End Sub
End Module
' The example displays output similar to the following:
' 5/3/2012 12:00:00 AM
'
' 5/3/2012
' Thursday, May 03, 2012
' 5/3/2012 12:00 AM
備註
從 .NET Framework 2.0 版本開始,回傳值為 , DateTime 其 Kind 屬性會返回 Local。
由於它回傳的是當前日期而非當前時間,因此 Today 該屬性適合用於僅使用日期的應用程式。 詳情請參閱 「DateTime、DateTimeOffset、TimeSpan 和 TimeZone Info 之間的選擇」。 相較之下, TimeOfDay 屬性會回傳當前時間但不含當前日期,且 Now 該物件會回傳當前日期與當前時間。