本文介绍了如何在c#中只显示小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我比较日期和时间时,如果超过24小时意味着它显示为例如25小时1.01:00:001天加1小时。我想要这样的结果25:00:00小时。



请帮帮我...

When i compare date & time, if more than 24 hours means it displays like example consider 25hours "1.01:00:00" 1 day plus 1 hour. i want result like this "25:00:00" hours.

Please help me...

推荐答案

DateTime now = DateTime.Now;
DateTime then = now.AddDays(-1).AddHours(-1);
TimeSpan diff = now - then;
Console.WriteLine("{0}:{1}:{2}", (int)diff.TotalHours, diff.Minutes, diff.Seconds);
DateTime random = new DateTime(2013, 2, 24, 7, 56, 23);
diff = now - random;
Console.WriteLine("{0}:{1}:{2}", (int)diff.TotalHours, diff.Minutes, diff.Seconds);

您需要将TotalHours转换为int,因为它将时间差作为double返回,其中分钟和秒作为小数部分。

You need to cast the TotalHours to an int, as it returns the difference in time as a double, with the minutes and seconds as the fractional part.


这篇关于如何在c#中只显示小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:17