C# 查找两个时间间隔之间的差异的问题
如何找出两个时间间隔之间的差异。 比如13:45:26.836-14:24:18.473,格式为“小时:分钟:秒:毫秒”。现在我需要找出这两次之间的时差 我怎样才能在C#中做到这一点C# 查找两个时间间隔之间的差异的问题,c#,visual-studio-2005,time,C#,Visual Studio 2005,Time,如何找出两个时间间隔之间的差异。 比如13:45:26.836-14:24:18.473,格式为“小时:分钟:秒:毫秒”。现在我需要找出这两次之间的时差 我怎样才能在C#中做到这一点 提前感谢。查找秒数;把这两个数字都减去,你就可以算出时差了。根据您使用的编程语言,我确信他们的库一定能够处理它。找到秒数;把这两个数字都减去,你就可以算出时差了。根据您使用的编程语言,我肯定他们的库一定能够处理它。我正在发布一个示例 你可以检查它并调整你的程序 /* Read the initial time. *
提前感谢。查找秒数;把这两个数字都减去,你就可以算出时差了。根据您使用的编程语言,我确信他们的库一定能够处理它。找到秒数;把这两个数字都减去,你就可以算出时差了。根据您使用的编程语言,我肯定他们的库一定能够处理它。我正在发布一个示例 你可以检查它并调整你的程序
/* Read the initial time. */
DateTime startTime = DateTime.Now;
Console.WriteLine(startTime);
/* Do something that takes up some time. For example sleep for 1.7 seconds. */
Thread.Sleep(1700);
/* Read the end time. */
DateTime stopTime = DateTime.Now;
Console.WriteLine(stopTime);
/* Compute the duration between the initial and the end time.
* Print out the number of elapsed hours, minutes, seconds and milliseconds. */
TimeSpan duration = stopTime - startTime;
Console.WriteLine("hours:" + duration.Hours);
Console.WriteLine("minutes:" + duration.Minutes);
Console.WriteLine("seconds:" + duration.Seconds);
Console.WriteLine("milliseconds:" + duration.Milliseconds);
我在贴一个例子 你可以检查它并调整你的程序
/* Read the initial time. */
DateTime startTime = DateTime.Now;
Console.WriteLine(startTime);
/* Do something that takes up some time. For example sleep for 1.7 seconds. */
Thread.Sleep(1700);
/* Read the end time. */
DateTime stopTime = DateTime.Now;
Console.WriteLine(stopTime);
/* Compute the duration between the initial and the end time.
* Print out the number of elapsed hours, minutes, seconds and milliseconds. */
TimeSpan duration = stopTime - startTime;
Console.WriteLine("hours:" + duration.Hours);
Console.WriteLine("minutes:" + duration.Minutes);
Console.WriteLine("seconds:" + duration.Seconds);
Console.WriteLine("milliseconds:" + duration.Milliseconds);
这将产生以下结果:
这将产生以下结果:
基本上,您需要做的是将这些时间值放入
DateTime
结构中。一旦有了两个DateTime
变量,只需将它们相减即可-结果是TimeSpan
类型的变量:
DateTime dt1 = new DateTime(2010, 5, 7, 13, 45, 26, 836);
DateTime dt2 = new DateTime(2010, 5, 7, 14, 24, 18, 473);
TimeSpan result = dt2 - dt1;
string result2 = result.ToString();
TimeSpan有很多属性可以获取集合,它们之间的差异可以用各种单位表示,例如毫秒、秒、分钟等。您也可以对其执行.ToString()
,以获取结果的字符串表示形式。在result2
中,您将看到如下内容:
00:38:51.6370000
这就是您要寻找的吗?基本上,您需要做的是将这些时间值放入
DateTime
结构中。一旦有了两个DateTime
变量,只需将它们相减即可-结果是TimeSpan
类型的变量:
DateTime dt1 = new DateTime(2010, 5, 7, 13, 45, 26, 836);
DateTime dt2 = new DateTime(2010, 5, 7, 14, 24, 18, 473);
TimeSpan result = dt2 - dt1;
string result2 = result.ToString();
TimeSpan有很多属性可以获取集合,它们之间的差异可以用各种单位表示,例如毫秒、秒、分钟等。您也可以对其执行.ToString()
,以获取结果的字符串表示形式。在result2
中,您将看到如下内容:
00:38:51.6370000
这就是你要找的吗