[Solved] Getting incorrect timings while calculating difference between 2 datetime


As has been commented on – the problem is due to the millisecond accuracy. You can subtract the millisecond by adding the negative of the milliseconds:

DateTime end = new DateTime(2018, 04, 13, 12, 17, 39, 067);
DateTime start = new DateTime(2018, 04, 13, 12, 17, 38, 893);
var diff = end.AddMilliseconds(-end.Millisecond) - start.AddMilliseconds(-start.Millisecond);
string s = string.Format("{0:mm} : {0:ss}", diff);
Console.WriteLine (s); // 00:01

Note if the end value were 38seconds & 967milliseconds for example – so within the same actual second – then 00:00 would be displayed.

7

solved Getting incorrect timings while calculating difference between 2 datetime