如何添加两组时间?例如00:10:10 + 00:00:20 = 00:10:30

时间:2014-11-27 01:19:38

标签: c# winforms

以下代码是如何获得两组时间之间的差异。 。但是加法怎么样? 请帮忙 。 。

private void button1_Click(object sender, EventArgs e)
    {
        DateTime dFrom;
        DateTime dTo;
        string sDateFrom = "00:10:38";
        string sDateTo = "00:00:04";
        if (DateTime.TryParse(sDateFrom, out dFrom) && DateTime.TryParse(sDateTo, out dTo))
        {
            TimeSpan TS = dFrom - dTo;
            int hour = TS.Hours;
            int mins = TS.Minutes;
            int secs = TS.Seconds;
  string timeDiff = hour.ToString("00") + ":" + mins.ToString("00") + ":" +  secs.ToString("00");
            textBox1.Text = timeDiff;
        }

    }

1 个答案:

答案 0 :(得分:2)

似乎TimeSpans更适合这里:

TimeSpan tsFrom;
TimeSpan tsTo;

string sFrom = "00:10:38";
string sTo = "00:00:04";

if (TimeSpan.TryParse(sFrom, out tsFrom) && TimeSpan.TryParse(sTo, out tsTo))
{
    TimeSpan ts = tsFrom + tsTo; // 00:10:42
}