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. 它还说明了如何使用某些标准日期和时间格式字符串设置 @no__t 0 值的格式。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 开始,返回值为 DateTime,其 @no__t 属性返回 LocalStarting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property returns Local.

由于它在不使用当前时间的情况下返回当前日期,因此 @no__t 的属性适用于仅使用日期的应用程序。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.

适用于

另请参阅