DateTime.Date DateTime.Date DateTime.Date DateTime.Date Property

Definición

Obtiene el componente correspondiente a la fecha de esta instancia.Gets the date component of this instance.

public:
 property DateTime Date { DateTime get(); };
public DateTime Date { get; }
member this.Date : DateTime
Public ReadOnly Property Date As DateTime

Valor de propiedad

Nuevo objeto con la misma fecha que esta instancia y con el valor de hora establecido en la medianoche (00:00:00).A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).

Ejemplos

En el ejemplo siguiente se usa el Date propiedad para extraer el componente de fecha un DateTime valor con el componente de hora se establece en cero (o 0:00:00 o medianoche).The following example uses the Date property to extract the date component of a DateTime value with its time component set to zero (or 0:00:00, or midnight). También explica que, dependiendo de la cadena de formato utilizada para mostrar el DateTime valor, el componente de hora puede seguir apareciendo en la salida con formato.It also illustrates that, depending on the format string used when displaying the DateTime value, the time component can continue to appear in formatted output.

using namespace System;

void main()
{
   DateTime^ date1 = gcnew DateTime(2008, 6, 1, 7, 47, 0);
   Console::WriteLine(date1->ToString());
   
   // Get date-only portion of date, without its time.
   DateTime dateOnly = date1->Date;
   // Display date using short date string.
   Console::WriteLine(dateOnly.ToString("d"));
   // Display date using 24-hour clock.
   Console::WriteLine(dateOnly.ToString("g"));
   Console::WriteLine(dateOnly.ToString(L"MM/dd/yyyy HH:mm"));   
}
// The example displays the following output to the console:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00
using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
      Console.WriteLine(date1.ToString());
      
      // Get date-only portion of date, without its time.
      DateTime dateOnly = date1.Date;
      // Display date using short date string.
      Console.WriteLine(dateOnly.ToString("d"));
      // Display date using 24-hour clock.
      Console.WriteLine(dateOnly.ToString("g"));
      Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
   }
}
// The example displays output like the following output:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00
Module Example
   Public Sub Main()
      Dim date1 As Date = #6/1/2008 7:47AM#
      Console.WriteLine(date1.ToString())
      
      ' Get date-only portion of date, without its time.
      Dim dateOnly As Date = date1.Date
      ' Display date using short date string.
      Console.WriteLine(dateOnly.ToString("d"))
      ' Display date using 24-hour clock.
      Console.WriteLine(dateOnly.ToString("g"))
      Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"))   
   End Sub
End Module
' The example displays output like the following:
'       6/1/2008 7:47:00 AM                     
'       6/1/2008
'       6/1/2008 12:00 AM
'       06/01/2008 00:00

Comentarios

El valor de la Kind propiedad devuelto del DateTime valor es el mismo que el de la instancia actual.The value of the Kind property of the returned DateTime value is the same as that of the current instance.

Dado que el DateTime tipo representa tanto las fechas y horas en un solo tipo, es importante evitar interpretar erróneamente una fecha devuelta por la Date propiedad como una fecha y hora.Because the DateTime type represents both dates and times in a single type, it is important to avoid misinterpreting a date returned by the Date property as a date and time.

Se aplica a