DateTime.Today 屬性

定義

取得目前的日期。Gets the current date.

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

屬性值

物件,設定為今天的日期,且時間元件設定為 00:00:00。An object that is set to today's date, with the time component set to 00:00:00.

範例

下列範例會使用 Date 屬性來取出目前的日期。The following example uses the Date property to retrieve the current date. 它也會說明如何使用某些標準日期和時間格式字串來格式化 DateTime 值。It also illustrates how a DateTime value can be formatted using some of the standard date and time format strings. 請注意,第三次呼叫 ToString(String) 方法所產生的輸出會使用 g 格式規範來包含時間元件,這是零。Note that the output produced by the third call to the ToString(String) method uses the g format specifier to include the time component, which is zero.

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
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 開始,傳回值是 Kind 屬性傳回 LocalDateTimeStarting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property returns Local.

由於它會傳回目前的日期,而不會傳回目前的時間,因此 Today 屬性適用于僅使用日期的應用程式。Because it returns the current date without the current time, the Today property is suitable for use in applications that work with dates only. 如需詳細資訊,請參閱在 DateTime、DateTimeOffset、TimeSpan 和 TimeZoneInfo 之間選擇For details, see Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo. 相反地,TimeOfDay 屬性會傳回目前的時間,但不含目前的日期,而 Now 屬性會傳回目前的日期和目前的時間。In contrast, the TimeOfDay property returns the current time without the current date, and the Now property returns both the current date and the current time.

適用於

另請參閱