subtract datetime.now from a datetime.now and truncat the millisecond

时间:2015-09-01 22:58:37

标签: c# asp.net datetime

I have the following inside my asp.net mvc web application:-

 scan.StartDate = System.DateTime.Now;
//do long job....
 scan.EndDate = System.DateTime.Now;

    var duration = (scan.EndDate - scan.StartDate);

now the duration will be something such as 00:00:50.0250000 so my question is how i can truncate the miliseconds and show only hour:minute:seconds ?

2 个答案:

答案 0 :(得分:2)

Here is the code which shall remove the milliseconds.msdn.

DateTime StartDate = System.DateTime.Now;
//do long job....
DateTime EndDate = System.DateTime.Now;               
var duration = (EndDate - StartDate);     
string st=duration.ToString("hh\\:mm\\:ss");              

答案 1 :(得分:1)

使用此:

var result = string.Format("{0:D2}:{1:D2}:{2:D2}", duration.Hours, duration.Minutes, duration.Seconds);