DateTime.Date Właściwość

Definicja

Pobiera składnik daty tego wystąpienia.

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

Wartość właściwości

DateTime

Nowy obiekt o tej samej dacie co to wystąpienie i wartość godziny ustawiona na 12:00:00 północy (00:00:00:00).

Przykłady

W poniższym przykładzie użyto Date właściwości , aby wyodrębnić składnik DateTime daty wartości ze składnikiem godziny ustawionym na zero (lub 0:00:00 lub północ). Ilustruje również, że w zależności od ciągu formatu używanego podczas wyświetlania DateTime wartości składnik czasu może nadal pojawiać się w sformatowanych danych wyjściowych.

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
open System

let date1 = DateTime(2008, 6, 1, 7, 47, 0)
printfn $"{date1}"

// Get date-only portion of date, without its time.
let dateOnly = date1.Date
// Display date using short date string.
printfn $"{dateOnly:d}"
// Display date using 24-hour clock.
printfn $"{dateOnly:g}"
printfn $"""{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

Uwagi

Wartość Kind właściwości zwróconej DateTime wartości jest taka sama jak wartość bieżącego wystąpienia.

DateTime Ponieważ typ reprezentuje zarówno daty, jak i godziny w jednym typie, ważne jest, aby uniknąć błędnej interpretacji daty zwróconej przez Date właściwość jako daty i godziny.

Dotyczy