Επεξεργασία

Κοινή χρήση μέσω


DateTime.Today Property

Definition

Gets the current date.

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

Property Value

An object that is set to today's date, with the time component set to 00:00:00.

Examples

The following example uses the Date property to retrieve the current date. It also illustrates how a DateTime value can be formatted using some of the standard date and time format strings. 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
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

Remarks

Starting with the .NET Framework version 2.0, the return value is a DateTime whose Kind property returns Local.

Because it returns the current date without the current time, the Today property is suitable for use in applications that work with dates only. For details, see Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo. 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.

Applies to

See also