C#中的DateTimeOffset.AddMonths()方法
C#中的DateTimeOffset.AddMonths()方法用于向此实例的值添加指定数量的月份。
句法
语法如下 −
public DateTimeOffset AddMonths (int val);
Above中,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
广告