減法兩個時間格式,C#自定義時間格式
string _time_One = "08:30" ;
string _time_Two = "08:35" ;
string _timeInterval = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two)).Minutes.ToString();
它給我的結果5
,但我想告訴喜歡這種格式00:05
。
請告訴我如何格式化它。提前致謝 !
減法兩個時間格式,C#自定義時間格式
string _time_One = "08:30" ;
string _time_Two = "08:35" ;
string _timeInterval = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two)).Minutes.ToString();
它給我的結果5
,但我想告訴喜歡這種格式00:05
。
請告訴我如何格式化它。提前致謝 !
@Lloyd就在這裏,而是要澄清這對於你的情況:
string _time_One = "08:30" ;
string _time_Two = "08:35" ;
TimeSpan ts = DateTime.Parse(_time_One) - DateTime.Parse(_time_Two);
MessageBox.Show(String.Format("Time: {0:00}:{1:00}", ts.Hours, ts.Minutes));
我希望這有助於。
string _time_One = "08:30";
string _time_Two = "08:35";
string _timeInterval = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two)).Duration().ToString();
結果=> 00:05:00
嘗試這種情況:
string _time_One = "08:30";
string _time_Two = "08:35";
var span = (DateTime.Parse(_time_One) - DateTime.Parse(_time_Two));
string _timeInterval = string.Format("{0:hh\\:mm}", span);
請證明您瞭解問題以及您嘗試解決問題的方法。請參閱[MSDN:TimeSpan.ToString()](http://msdn.microsoft.com/en-us/library/dd992632(v = vs.110).aspx)。 – CodeCaster
可能重複[解析C#字符串到DateTime](http://stackoverflow.com/questions/7580809/parse-c-sharp-string-to-datetime) –
可能的重複[如何將TimeSpan轉換爲格式化字符串?](http://stackoverflow.com/questions/842057/how-do-i-convert-a-timespan-to-a-formatted-string) – CodeCaster