DateTimeOffset.Month 屬性

定義

取得目前 DateTimeOffset 物件所表示之日期的月份元件。

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

屬性值

目前 DateTimeOffset 物件的月份元件,以 1 到 12 之間的整數表示。

範例

下列範例會以三種不同的方式顯示值的月份元件 DateTimeOffset

DateTimeOffset theTime = new DateTimeOffset(2008, 9, 7, 11, 25, 0,
                                       DateTimeOffset.Now.Offset);
Console.WriteLine("The month component of {0} is {1}.",
                  theTime, theTime.Month);

Console.WriteLine("The month component of {0} is{1}.",
                  theTime, theTime.ToString(" M"));

Console.WriteLine("The month component of {0} is {1}.",
                  theTime, theTime.ToString("MM"));
// The example produces the following output:
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 09.
let theTime = DateTimeOffset(2008, 9, 7, 11, 25, 0, DateTimeOffset.Now.Offset)
printfn $"The month component of {theTime} is {theTime.Month}."

printfn $"""The month component of {theTime} is{theTime.ToString " M"}."""

printfn $"""The month component of {theTime} is {theTime.ToString "MM"}."""

// The example produces the following output:
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
//    The month component of 9/7/2008 11:25:00 AM -08:00 is 09.
Dim theTime As New DateTimeOffset(#9/7/2008 11:25AM#, _
                                       DateTimeOffset.Now.Offset)
Console.WriteLine("The month component of {0} is {1}.", _
                  theTime, theTime.Month)

Console.WriteLine("The month component of {0} is{1}.", _
                  theTime, theTime.ToString(" M"))

Console.WriteLine("The month component of {0} is {1}.", _
                  theTime, theTime.ToString("MM"))
' The example produces the following output:
'    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
'    The month component of 9/7/2008 11:25:00 AM -08:00 is 9.
'    The month component of 9/7/2008 11:25:00 AM -08:00 is 09.

備註

屬性 Month 不會受到 屬性的值 Offset 影響。

您也可以使用 「M」 或 「MM」 自訂格式規範呼叫 方法來建立 ToString 物件的月份元件的字串表示 DateTimeOffset

適用於