如何在c#中只显示小时数 [英] How to display only hours in c#

查看:208
本文介绍了如何在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对象时,你总是得到一个TimeSpan对象 - 它具有你需要的属性:

When you subtract two DateTime objects, you always get a TimeSpan object - this has the properties you need directly:
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#中只显示小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆