DateTimeOffset.AddMonths(Int32) 方法

定义

返回一个新 DateTimeOffset 对象,该对象将指定月数添加到此实例的值。

public:
 DateTimeOffset AddMonths(int months);
public DateTimeOffset AddMonths(int months);
member this.AddMonths : int -> DateTimeOffset
Public Function AddMonths (months As Integer) As DateTimeOffset

参数

months
Int32

整整几个月。 该数字可以是负数或正数。

返回

一个对象,其值为当前 DateTimeOffset 对象所表示的日期和时间和表示 months的月数之和。

例外

示例

以下示例使用 AddMonths 该方法显示 2007 年每个季度的开始日期。

DateTimeOffset quarterDate = new DateTimeOffset(2007, 1, 1, 0, 0, 0,
                                 DateTimeOffset.Now.Offset);
for (int ctr = 1; ctr <= 4; ctr++)
{
   Console.WriteLine("Quarter {0}: {1:MMMM d}", ctr, quarterDate);
   quarterDate = quarterDate.AddMonths(3);
}
// This example produces the following output:
//       Quarter 1: January 1
//       Quarter 2: April 1
//       Quarter 3: July 1
//       Quarter 4: October 1
let mutable quarterDate = DateTimeOffset(2007, 1, 1, 0, 0, 0, DateTimeOffset.Now.Offset)
for i = 1 to 4 do
    printfn $"""Quarter {i}: {quarterDate.ToString "MMMM d"}"""
    quarterDate <- quarterDate.AddMonths 3

// This example produces the following output:
//       Quarter 1: January 1
//       Quarter 2: April 1
//       Quarter 3: July 1
//       Quarter 4: October 1
Dim quarterDate As New DateTimeOffset(#01/01/2007#, DateTimeOffset.Now.Offset)
For ctr As Integer = 1 To 4
   Console.WriteLine("Quarter {0}: {1:MMMM d}", ctr, quarterDate)
   quarterDate = quarterDate.AddMonths(3)
Next   
' This example produces the following output:
'       Quarter 1: January 1
'       Quarter 2: April 1
'       Quarter 3: July 1
'       Quarter 4: October 1

注解

与将单个时间间隔单位(如分钟或天)添加到日期和时间值的大多数其他方法不同, AddMonths 不能添加月份的小数部分。 若要向对象添加由其他时间单位组成的时间,以及月份, DateTimeOffset 请使用 Add 该方法。

注释

此方法返回一个新 DateTimeOffset 对象。 它不会通过添加到 months 其日期和时间来修改当前对象的值。

适用于