简体   繁体   English

C#将天数转换为时间hh:mm

[英]c# convert days to hh:mm in timespan

while calculate the sum of all span it shows result in days (dd:HH:mm) .question is how i get the sum only in HH:mm format.how i convert the days in hour 同时计算所有跨度的总和它显示结果以天(dd:HH:mm)问题是我只有在获得和HH:mm format.how我的天转换成小时

          TimeSpan Span1 = TimeSpan.Parse(mnts1);
          TimeSpan Span2 = TimeSpan.Parse(tuts1);
          TimeSpan Span3 = TimeSpan.Parse(wdts1);
          TimeSpan Span4 = TimeSpan.Parse(thts1);
          TimeSpan Span5 = TimeSpan.Parse(frts1);
          TimeSpan Span6 = TimeSpan.Parse(stts1);
          TimeSpan Span7 = TimeSpan.Parse(suts1);
          TimeSpan  rf = Span1 + Span2 + Span3 + Span4 + Span5 + Span6 + Span7; 

Format it manually: 手动格式化:

Console.WriteLine("{0:00}:{1:00}", rf.TotalHours, rf.Minutes);

You can still use string formatting by calling string.Format : 您仍然可以通过调用string.Format来使用字符串格式:

string result = string.Format("{0:00}:{1:00}", rf.TotalHours, rf.Minutes);
Console.WriteLine(result);

There doesn't seem to be a format option for getting the total hours out of a TimeSpan . 似乎没有格式选项可用于获取TimeSpan的总时数。 Your best bet would be to use the TotalHours property instead: 最好的选择是改为使用TotalHours属性:

Console.WriteLine("{0:00}:{1:00}", (int)rf.TotalHours, rf.Minutes);

TotalHours returns a double as it includes the fractional hours so you need to truncate it to just the integer part. TotalHours返回一个double值,因为它包括小数小时,因此您需要将其截断为整数部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM