DateTimeOffset.Date Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a DateTime value that represents the date component of the current DateTimeOffset object.
public:
property DateTime Date { DateTime get(); };
public DateTime Date { get; }
member this.Date : DateTime
Public ReadOnly Property Date As DateTime
A DateTime value that represents the date component of the current DateTimeOffset object.
The following example retrieves the value of the Date property for a specific date. It then displays that value to the console using some standard and custom date-only format specifiers.
// Illustrate Date property and date formatting
DateTimeOffset thisDate = new DateTimeOffset(2008, 3, 17, 1, 32, 0, new TimeSpan(-5, 0, 0));
string fmt; // format specifier
// Display date only using "D" format specifier
// For en-us culture, displays:
// 'D' format specifier: Monday, March 17, 2008
fmt = "D";
Console.WriteLine("'{0}' format specifier: {1}",
fmt, thisDate.Date.ToString(fmt));
// Display date only using "d" format specifier
// For en-us culture, displays:
// 'd' format specifier: 3/17/2008
fmt = "d";
Console.WriteLine("'{0}' format specifier: {1}",
fmt, thisDate.Date.ToString(fmt));
// Display date only using "Y" (or "y") format specifier
// For en-us culture, displays:
// 'Y' format specifier: March, 2008
fmt = "Y";
Console.WriteLine("'{0}' format specifier: {1}",
fmt, thisDate.Date.ToString(fmt));
// Display date only using custom format specifier
// For en-us culture, displays:
// 'dd MMM yyyy' format specifier: 17 Mar 2008
fmt = "dd MMM yyyy";
Console.WriteLine("'{0}' format specifier: {1}",
fmt, thisDate.Date.ToString(fmt));
// Illustrate Date property and date formatting
let thisDate = DateTimeOffset(2008, 3, 17, 1, 32, 0, TimeSpan(-5, 0, 0))
// Display date only using "D" format specifier
// For en-us culture, displays:
// 'D' format specifier: Monday, March 17, 2008
let fmt = "D"
printfn $"'{fmt}' format specifier: {thisDate.Date.ToString fmt}"
// Display date only using "d" format specifier
// For en-us culture, displays:
// 'd' format specifier: 3/17/2008
let fmt = "d"
printfn $"'{fmt}' format specifier: {thisDate.Date.ToString fmt}"
// Display date only using "Y" (or "y") format specifier
// For en-us culture, displays:
// 'Y' format specifier: March, 2008
let fmt = "Y"
printfn $"'{fmt}' format specifier: {thisDate.Date.ToString fmt}"
// Display date only using custom format specifier
// For en-us culture, displays:
// 'dd MMM yyyy' format specifier: 17 Mar 2008
let fmt = "dd MMM yyyy"
printfn $"'{fmt}' format specifier: {thisDate.Date.ToString fmt}"
' Illustrate Date property and date formatting
Dim thisDate As New DateTimeOffset(#3/17/2008 1:32AM#, New TimeSpan(-5, 0, 0))
Dim fmt As String ' format specifier
' Display date only using "D" format specifier
' For en-us culture, displays:
' 'D' format specifier: Monday, March 17, 2008
fmt = "D"
Console.WriteLine("'{0}' format specifier: {1}", _
fmt, thisDate.Date.ToString(fmt))
' Display date only using "d" format specifier
' For en-us culture, displays:
' 'd' format specifier: 3/17/2008
fmt = "d"
Console.WriteLine("'{0}' format specifier: {1}", _
fmt, thisDate.Date.ToString(fmt))
' Display date only using "Y" (or "y") format specifier
' For en-us culture, displays:
' 'Y' format specifier: March, 2008
fmt = "Y"
Console.WriteLine("'{0}' format specifier: {1}", _
fmt, thisDate.Date.ToString(fmt))
' Display date only using custom format specifier
' For en-us culture, displays:
' 'dd MMM yyyy' format specifier: 17 Mar 2008
fmt = "dd MMM yyyy"
Console.WriteLine("'{0}' format specifier: {1}", _
fmt, thisDate.Date.ToString(fmt))
This property removes any significant part of the time component from a DateTimeOffset object and returns only its significant date component. For example, if the DateTimeOffset object has a date and time value of "1/12/07 4:01pm +7:30", the property returns a DateTime value of "1/12/07 12:00:00 AM". The DateTime value can then be displayed by using any of the standard or custom format specifiers that display dates only. (See the Example section for an illustration.)
The value of the DateTime.Kind property of the returned DateTime object is always DateTimeKind.Unspecified. It is not affected by the value of the Offset property.
To display a date without its time component, you can also use the "D" or "d" format specifiers; for an illustration, see the Example section.
Product | Versions |
---|---|
.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 |
.NET Framework | 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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: