Compartir a través de


DateTime.Date Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the date component of this instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public ReadOnly Property Date As DateTime
public DateTime Date { get; }

Property Value

Type: System.DateTime
A new object that has the same date as this instance, and its time value set to 12:00:00 midnight (00:00:00).

Examples

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). 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.

Dim date1 As Date = #6/1/2008 7:47:00 AM#
outputBlock.Text += date1.ToString() & vbCrLf

' Get date-only portion of date, without its time.
Dim dateOnly As Date = date1.Date
' Display date using short date string.
outputBlock.Text += dateOnly.ToString("d") & vbCrLf
' Display date using 24-hour clock.
outputBlock.Text += dateOnly.ToString("g") & vbCrLf
outputBlock.Text += dateOnly.ToString("MM/dd/yyyy HH:mm") & vbCrLf
' The example displays the following output:
'       6/1/2008 7:47:00 AM
'       6/1/2008
'       6/1/2008 12:00 AM
'       06/01/2008 00:00
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
outputBlock.Text += date1.ToString() + "\n";

// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
outputBlock.Text += dateOnly.ToString("d") + "\n";
// Display date using 24-hour clock.
outputBlock.Text += dateOnly.ToString("g") + "\n";
outputBlock.Text += dateOnly.ToString("MM/dd/yyyy HH:mm") + "\n";
// The example displays the following output:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.