Leggi in inglese

Condividi tramite


DateTime.Today Proprietà

Definizione

Ottiene la data corrente.

C#
public static DateTime Today { get; }

Valore della proprietà

Oggetto che è impostato sulla data corrente, con il componente ora impostato su 00.00.00.

Esempio

Nell'esempio seguente viene usata la Date proprietà per recuperare la data corrente. Illustra anche come è possibile formattare un DateTime valore usando alcune delle stringhe di formato data e ora standard. Si noti che l'output prodotto dalla terza chiamata al ToString(String) metodo usa l'identificatore di formato g per includere il componente time, ovvero zero.

C#
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

Commenti

A partire da .NET Framework versione 2.0, il valore restituito è un DateTime oggetto la cui Kind proprietà restituisce Local.

Poiché restituisce la data corrente senza l'ora corrente, la Today proprietà è adatta per l'uso nelle applicazioni che funzionano solo con date. Per informazioni dettagliate, vedere Scelta tra DateTime, DateTimeOffset, TimeSpan e TimeZoneInfo. Al contrario, la TimeOfDay proprietà restituisce l'ora corrente senza la data corrente e la Now proprietà restituisce sia la data corrente che l'ora corrente.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche