Compartir vía


DateTime.Today Propiedad

Definición

Obtiene la fecha actual.

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

Valor de propiedad

Objeto que se establece en la fecha de hoy, con el componente de hora establecido en 00:00:00.

Ejemplos

En el ejemplo siguiente se usa la Date propiedad para recuperar la fecha actual. También muestra cómo se puede dar formato a un DateTime valor mediante algunas de las cadenas de formato de fecha y hora estándar. Tenga en cuenta que la salida generada por la tercera llamada al ToString(String) método usa el especificador de formato g para incluir el componente de hora, que es cero.

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

Comentarios

A partir de .NET Framework versión 2.0, el valor devuelto es un DateTime cuya Kind propiedad devuelve Local.

Dado que devuelve la fecha actual sin la hora actual, la Today propiedad es adecuada para su uso en aplicaciones que solo funcionan con fechas. Para obtener más información, consulte Elegir entre DateTime, DateTimeOffset, TimeSpan y TimeZoneInfo. Por el contrario, la TimeOfDay propiedad devuelve la hora actual sin la fecha actual y la Now propiedad devuelve tanto la fecha actual como la hora actual.

Se aplica a

Consulte también