简体   繁体   English

如何以自定义格式显示汇总的TimeSpans?

[英]How to display summed TimeSpans in a custom format?

I need your help with following problem: 对于以下问题,我需要您的帮助:

I have an ASP.net Grid View and I want to sum times in gridview on cell text and if result is more than 24 Hours then I don't want Days appearing, I need only hours and minutes ( 45:12 ). 我有一个ASP.net网格视图,我想对单元格文本上的网格视图中的时间求和,如果结果超过24小时,那么我不希望出现几天,我只需要小时和分钟(45:12)。 I'm using following code. 我正在使用以下代码。 Please Help. 请帮忙。

protected void grvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TimeSpan sum3 = TimeSpan.Zero;
    if (e.Row.RowType==DataControlRowType.DataRow)
    {
        string tt3 = e.Row.Cells[4].Text;

        sum3 += TimeSpan.Parse(tt3);

        llate.Text = string.Format("{0}:{1}", (int)sum3.TotalHours, sum3.Minutes);
    }
} 

To add together TimeSpan s and format the result as total hours and minutes, try something like the following: 要将TimeSpan相加并将结果格式化为总时数和分钟数,请尝试以下操作:

TimeSpan ts1 = TimeSpan.Parse("21:15");
TimeSpan ts2 = TimeSpan.Parse("14.23:02");
ts1 += ts2;

string hoursMinutes = string.Format("{0:00}:{1:00}", ts1.TotalHours, ts1.Minutes);

use like this: 像这样使用:

var j = string.Format("{0:00}",1); //output : 01
var j = string.Format("{0:00}",10); //output : 10

Can use this too: 也可以使用此:

var i = 10.ToString().PadLeft(2, '0'); //output : 10
var i = 1.ToString().PadLeft(2, '0'); //output : 01

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

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