DateTimeOffset.Year 属性
定义
获取由当前 DateTimeOffset 对象所表示的日期的年份组成部分。Gets the year component of the date represented by the current DateTimeOffset object.
public:
property int Year { int get(); };
public int Year { get; }
member this.Year : int
Public ReadOnly Property Year As Integer
属性值
当前 DateTimeOffset 对象的年份组成部分,以 0 到 9999 之间的一个整数值来表示。The year component of the current DateTimeOffset object, expressed as an integer value between 0 and 9999.
示例
下面的示例 DateTimeOffset 以四种不同的方式显示值的年份部分:The following example displays the year component of a DateTimeOffset value in four different ways:
通过检索属性的值 Year 。By retrieving the value of the Year property.
通过 ToString(String) 使用 "y" 格式说明符调用方法。By calling the ToString(String) method with the "y" format specifier.
通过 ToString(String) 使用 "yy" 格式说明符调用方法。By calling the ToString(String) method with the "yy" format specifier.
通过 ToString(String) 使用 "yyyy" 格式说明符调用方法。By calling the ToString(String) method with the "yyyy" format specifier.
DateTimeOffset theTime = new DateTimeOffset(2008, 2, 17, 9, 0, 0,
DateTimeOffset.Now.Offset);
Console.WriteLine("The year component of {0} is {1}.",
theTime, theTime.Year);
Console.WriteLine("The year component of {0} is{1}.",
theTime, theTime.ToString(" y"));
Console.WriteLine("The year component of {0} is {1}.",
theTime, theTime.ToString("yy"));
Console.WriteLine("The year component of {0} is {1}.",
theTime, theTime.ToString("yyyy"));
// The example produces the following output:
// The year component of 2/17/2008 9:00:00 AM -07:00 is 2008.
// The year component of 2/17/2008 9:00:00 AM -07:00 is 8.
// The year component of 2/17/2008 9:00:00 AM -07:00 is 08.
// The year component of 2/17/2008 9:00:00 AM -07:00 is 2008.
Dim theTime As New DateTimeOffset(#2/17/2008 9:00AM#, _
DateTimeOffset.Now.Offset)
Console.WriteLine("The year component of {0} is {1}.", _
theTime, theTime.Year)
Console.WriteLine("The year component of {0} is{1}.", _
theTime, theTime.ToString(" y"))
Console.WriteLine("The year component of {0} is {1}.", _
theTime, theTime.ToString("yy"))
Console.WriteLine("The year component of {0} is {1}.", _
theTime, theTime.ToString("yyyy"))
' The example produces the following output:
' The year component of 2/17/2008 9:00:00 AM -07:00 is 2008.
' The year component of 2/17/2008 9:00:00 AM -07:00 is 8.
' The year component of 2/17/2008 9:00:00 AM -07:00 is 08.
' The year component of 2/17/2008 9:00:00 AM -07:00 is 2008.
注解
Year属性的值不会影响属性 Offset 。The Year property is not affected by the value of the Offset property.
您还可以 DateTimeOffset 通过 ToString 使用 "y"、"yy" 或 "yyyy" 自定义格式说明符调用方法,创建对象的年组成部分的字符串表示形式。You can also create a string representation of a DateTimeOffset object's year component by calling the ToString method with the "y", "yy", or "yyyy" custom format specifiers.