Simon Tindall Simon Tindall - 1 year ago 75
C# Question

Get date difference for differences > 24 hours

I am displaying on my web app the difference between two dates in

format using the following code:

double durationSeconds = (EndDateTime - StartDateTime).TotalSeconds;
TimeSpan seconds = TimeSpan.FromSeconds(durationSeconds);
string duration = seconds.ToString(@"hh\:mm\:ss\:fff");
litDuration.Text = duration;

This only works for differences less than 24 hours but I would like the hour counter to display higher. e.g. using the following DateTimes

StartDateTime = 2016-08-25 15:00:00
EndDateTime = 2016-08-27 15:28:30

The difference in hh:mm:ss format should be
but is currently displaying

Any help is greatly appreciated

Answer Source

How about doing it this way?

string duration =
        seconds.Days * 24.0 + seconds.Hours,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download