DateTimeOffset.Year 属性

定义

获取由当前 DateTimeOffset 对象所表示的日期的年份组成部分。

public:
 property int Year { int get(); };
public int Year { get; }
member this.Year : int
Public ReadOnly Property Year As Integer

属性值

Int32

当前 DateTimeOffset 对象的年份组成部分,以 0 到 9999 之间的一个整数值来表示。

示例

以下示例以四种不同的方式显示值年份组件 DateTimeOffset

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.

注解

Year 属性不受该属性的值 Offset 的影响。

还可以通过使用“y”、“yy”或“yyy”自定义格式说明符调用ToString方法来创建对象的年份组件的字符串表示DateTimeOffset形式。

适用于