C#中的DateTime.AddMinutes()方法
C#中的DateTime.AddMinutes()方法用于将指定的分钟数添加到此实例的值。它返回新的DateTime。
语法
以下是语法-
public DateTime AddMinutes (double m);
上面的m是要添加的分钟。如果添加负值,则分钟数将被减去。
示例
现在让我们看一个实现DateTime.AddMinutes()方法的示例-
using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25); DateTime d2 = d1.AddMinutes(25); System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); System.Console.WriteLine("\nNew DateTime (After adding minutes) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); } }
输出结果
这将产生以下输出-
Initial DateTime = 10 September 2019, 05:15:25 New DateTime (After adding minutes) = 10 September 2019, 05:40:25
示例
现在让我们来看另一个实现DateTime.AddMinutes()方法的示例-
using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25); DateTime d2 = d1.AddMinutes(-15); System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); System.Console.WriteLine("\nNew DateTime (After subtracting minutes) = {0:dd} {0:y}, {0:hh}: {0:mm}:{0:ss} ", d2); } }
输出结果
这将产生以下输出-
Initial DateTime = 10 September 2019, 05:15:25 New DateTime (After subtracting minutes) = 10 September 2019, 05:00:25