DateTimeOffset.Year 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取由当前 DateTimeOffset 对象所表示的日期的年份组成部分。
public:
property int Year { int get(); };
public int Year { get; }
member this.Year : int
Public ReadOnly Property Year As Integer
属性值
当前 DateTimeOffset 对象的年份组成部分,以 0 到 9999 之间的一个整数值来表示。
示例
以下示例以四种不同的方式显示值年份组件 DateTimeOffset :
通过检索属性的值 Year 。
通过使用“y”格式说明符调用 ToString(String) 方法。
使用“yy”格式说明符调用 ToString(String) 方法。
通过使用“yyyy”格式说明符调用 ToString(String) 方法。
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.
let theTime = DateTimeOffset(2008, 2, 17, 9, 0, 0, DateTimeOffset.Now.Offset)
printfn $"The year component of {theTime} is {theTime.Year}."
printfn $"""The year component of {theTime} is{theTime.ToString " y"}."""
printfn $"The year component of {theTime} is {theTime:yy}."
printfn $"The year component of {theTime} is {theTime: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.
注解
还可以通过使用“y”、“yy”或“yyy”自定义格式说明符调用ToString方法来创建对象的年份组件的字符串表示DateTimeOffset形式。