次の方法で共有


DateTimeOffset.Year プロパティ

定義

現在の DateTimeOffset オブジェクトによって表される日付の年コンポーネントを取得します。

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

プロパティ値

現在の DateTimeOffset オブジェクトの year コンポーネント。0 ~ 9999 の整数値で表されます。

次の例では、 DateTimeOffset 値の年の構成要素を 4 つの異なる方法で表示します。

  • 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.

注釈

Year プロパティは、Offset プロパティの値の影響を受けません。

また、"y"、"yy"、または "y" カスタム書式指定子を使用して ToString メソッドを呼び出すことによって、DateTimeOffset オブジェクトの年コンポーネントの文字列表現を作成することもできます。

適用対象