如何用累计小时显示TimeSpan?

时间:2017-04-05 14:22:55

标签: c# winforms formatting timespan

我从外部源接收导入,其中以秒为单位存储时间跨度字段。 要将这些转换为时间跨度,我目前正在执行此操作

TimeSpan.FromSeconds(140520)

这会导致显示1 15:02:00的时间跨度 我想要的是显示39:02:00

我该怎么做?

1 个答案:

答案 0 :(得分:3)

生成的TimeSpan的TotalHours属性应该为您提供所需内容的第一部分:

var timespan = TimeSpan.FromSeconds(140520);
//If you cast the TotalHours to int you will get '39'
int totalHours = (int)timespan.TotalHours;

//If you want the '2' that would result from your input of 140520 you will need the minutes property
int minutes = timespan.Minutes;
相关问题