2011-12-01 26 views
0

如何以秒爲單位格式化輸出:毫秒格式?如何將字符串轉換爲時間對象進行計算

TimeSpan start = TimeSpan.Parse(pair.Value[3]); 
TimeSpan end = TimeSpan.Parse(pair.Value[4]); 
Console.WriteLine(TimeSpan.Compare(start,end)); 

此處的代碼以秒爲單位打印差異。我怎樣才能解決它使用string.format,因爲我不知道約毫秒?

+1

TimeSpan.Compare函數不會以秒爲單位返回差值。它返回-1,0或1. – xbrady

回答

2

你需要看兩個時間跨度之間的差別在。

TimeSpan start = new TimeSpan(42); // 42 ticks 
TimeSpan end = new TimeSpan(420000000); 
TimeSpan diff = end.Subtract(start); 
string ms = diff.Milliseconds.ToString(); 
string sec = ((int)diff.TotalSeconds).ToString(); 
Console.WriteLine(sec + ":" + ms); 
2

如何

Console.WriteLine(end.Subtract(start).TotalMilliseconds) 
+0

不提供請求的秒數:毫秒格式。 –