C#中的DateTimeOffset.AddMonths()方法用于将指定的月数添加到此实例的值。
以下是语法-
public DateTimeOffset AddMonths (int val);
上面的Val是要添加的月份数。要减去月份,请设置负值。
现在让我们看一个实现DateTimeOffset.AddMonths()方法的示例-
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0)); Console.WriteLine("DateTimeOffset (before adding months) = {0}", dateTimeOffset); DateTimeOffset res = dateTimeOffset.AddMonths(3); Console.WriteLine("DateTimeOffset (after adding months) = {0}", res); } }
输出结果
这将产生以下输出-
DateTimeOffset (before adding months) = 8/10/2019 4:20:10 AM -05:00 DateTimeOffset (after adding months) = 11/10/2019 4:20:10 AM -05:00
现在让我们来看另一个实现DateTimeOffset.AddMonths()方法的示例-
using System; public class Demo { public static void Main() { DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 08, 10, 4, 20, 10, new TimeSpan(-5, 0, 0)); Console.WriteLine("DateTimeOffset (before subtracting months) = {0}", dateTimeOffset); DateTimeOffset res = dateTimeOffset.AddMonths(-5); Console.WriteLine("DateTimeOffset (after subtracting months) = {0}", res); } }
输出结果
这将产生以下输出-
DateTimeOffset (before subtracting months) = 8/10/2019 4:20:10 AM -05:00 DateTimeOffset (after subtracting months) = 3/10/2019 4:20:10 AM -05:00