通过


DateTime.Today 属性

定义

获取当前日期。

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 开始,返回值是 DateTimeKind 属性返回 Local的值。

由于该属性返回当前日期而不返回当前时间,因此 Today 该属性适用于仅处理日期的应用程序。 有关详细信息,请参阅 “在 DateTime、DateTimeOffset、TimeSpan 和 TimeZoneInfo 之间进行选择”。 相反,该 TimeOfDay 属性返回没有当前日期的当前时间,并且 Now 该属性同时返回当前日期和当前时间。

适用于

另请参阅