如何以秒爲單位格式化輸出:毫秒格式?如何將字符串轉換爲時間對象進行計算
TimeSpan start = TimeSpan.Parse(pair.Value[3]);
TimeSpan end = TimeSpan.Parse(pair.Value[4]);
Console.WriteLine(TimeSpan.Compare(start,end));
此處的代碼以秒爲單位打印差異。我怎樣才能解決它使用string.format,因爲我不知道約毫秒?
如何以秒爲單位格式化輸出:毫秒格式?如何將字符串轉換爲時間對象進行計算
TimeSpan start = TimeSpan.Parse(pair.Value[3]);
TimeSpan end = TimeSpan.Parse(pair.Value[4]);
Console.WriteLine(TimeSpan.Compare(start,end));
此處的代碼以秒爲單位打印差異。我怎樣才能解決它使用string.format,因爲我不知道約毫秒?
你需要看兩個時間跨度之間的差別在。
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);
如果是在幾秒鐘內,你可以乘以1000的答案,並寫出一個字符串?
否則,看看這個MSDN文章http://msdn.microsoft.com/en-us/library/bb882581.aspx
如何
Console.WriteLine(end.Subtract(start).TotalMilliseconds)
不提供請求的秒數:毫秒格式。 –
TimeSpan.Compare函數不會以秒爲單位返回差值。它返回-1,0或1. – xbrady