C#中的DateTime.AddMilliseconds()方法
C#中的DateTime.AddMilliseconds()方法用于将指定的毫秒数添加到此实例的值。此方法返回一个新的DateTime。
语法
以下是语法-
public DateTime AddMilliseconds (double mSec);
上面的毫秒是要添加的毫秒数。
示例
现在让我们看一个实现DateTime.AddMilliseconds()方法的示例-
using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 11, 10, 5, 0, 25); DateTime d2 = d1.AddMilliseconds(1000); System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); System.Console.WriteLine("\nNew DateTime (After adding milliseconds) = {0:dd} {0:y}, {0:hh}: {0:mm}:{0:ss} ", d2); } }
输出结果
这将产生以下输出-
Initial DateTime = 10 November 2019, 05:00:25 New DateTime (After adding milliseconds) = 10 November 2019, 05:00:26
示例
现在让我们来看另一个实现DateTime.AddMilliseconds()方法的示例-
using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 10, 5, 9, 0, 10); DateTime d2 = d1.AddMilliseconds(-2000); System.Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); System.Console.WriteLine("\nNew DateTime (After subtracting milliseconds) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); } }
输出结果
这将产生以下输出-
Initial DateTime = 05 October 2019, 09:00:10 New DateTime (After subtracting milliseconds) = 05 October 2019, 09:00:08