3

我试图在 c# 中将两个日期相减。我当前的代码如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    System.DateTime matchStart = new System.DateTime(2012, 10, 17, 20, 00, 00);
    System.DateTime currentDateTime = new System.DateTime(2012, 10, 9, 14, 00, 00);

    System.TimeSpan matchCountdown = matchStart.Subtract(currentDateTime);

    countdown.Text = matchCountdown.ToString();
}

这目前给了我结果“8.06:00:00”。然而,我想要做的是将时差格式化为“8 天,6 小时,0 分钟”。我到底该怎么做呢?

任何帮助深表感谢!

4

1 回答 1

5

您可以使用String.FormatTimeSpan属性

String.Format("{0} days, {1} hours, {2} minutes"
    , matchCountdown.Days
    , matchCountdown.Hours
    , matchCountdown.Minutes); 

这是演示:http: //ideone.com/i8SKX

于 2012-10-09T09:12:14.650 回答