以下代码是如何获得两组时间之间的差异。 。但是加法怎么样? 请帮忙 。 。
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;
}
}
答案 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
}