DateTimeOffset.Date 属性

定义

获取 DateTime 值,该值表示当前 DateTimeOffset 对象的日期组成部分。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

属性值

一个 DateTime 值,该值表示当前 DateTimeOffset 对象的日期组成部分。A DateTime value that represents the date component of the current DateTimeOffset object.

示例

下面的示例检索特定日期的 Date 属性的值。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
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))

注解

此属性将从 DateTimeOffset 对象中移除时间部分的任何重要部分,并只返回其重要的日期部分。This property removes any significant part of the time component from a DateTimeOffset object and returns only its significant date component. 例如,如果 DateTimeOffset 对象的日期和时间值为 "1/12/07 4: 01pm + 7:30",则属性将返回 DateTime 值 "1/12/07 12:00:00 AM"。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". 然后,可以使用仅显示日期的任何标准或自定义格式说明符来显示 DateTime 值。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.)

返回的 DateTime 对象的 DateTime.Kind 属性的值始终是 DateTimeKind.Unspecified的。The value of the DateTime.Kind property of the returned DateTime object is always DateTimeKind.Unspecified. 它不受 Offset 属性的值的影响。It is not affected by the value of the Offset property.

若要显示日期而不显示其时间组成部分,还可以使用 "D" 或 "d" 格式说明符;有关说明,请参阅 "示例" 部分。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.

适用于

另请参阅