Hum*_*lly 2 c# formatting datetime
我想在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();
}
Run Code Online (Sandbox Code Playgroud)
这目前给我的结果是"8.06:00:00".然而,我想要做的是将时间差格式化为"8天,6小时,0分钟".我怎么能这样做呢?
任何帮助深表感谢!
您可以使用String.Format和TimeSpan属性.
String.Format("{0} days, {1} hours, {2} minutes"
, matchCountdown.Days
, matchCountdown.Hours
, matchCountdown.Minutes);
Run Code Online (Sandbox Code Playgroud)
这是演示:http://ideone.com/i8SKX