3

I am displaying on my web app the difference between two dates in hh:mm:ss 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 48:28:30 but is currently displaying 00:28:30.

Any help is greatly appreciated

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Simon
  • 1,293
  • 5
  • 21
  • 39
  • 3
    Take a look at this post if it could help you http://stackoverflow.com/questions/3505230/format-timespan-greater-than-24-hour – Bojan B Aug 26 '16 at 10:31
  • `seconds.ToString(@"hh\:mm\:ss\:fff");` will use `seconds.Hours` for formatting the output. Whereas you want to use `seconds.TotalHours`. You will need to write your own formatting function for that (see link above for an example) – oleksii Aug 26 '16 at 10:35
  • 1
    You will never get a TimeSpan to display any number higher than 23 for Hours. Look at the **Days** property and add +24 hours when you convert to string – Camilo Terevinto Aug 26 '16 at 10:35

5 Answers5

4

How about doing it this way?

string duration =
    String.Format(
        @"{0}:{1:mm\:ss\:fff}",
        seconds.Days * 24.0 + seconds.Hours,
        seconds);
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Something like this?

var StartDateTime = new DateTime(2016, 8, 25, 15, 0, 0);
var EndDateTime = new DateTime(2016, 8, 27, 15, 28, 30);

var durationTimeSpan = EndDateTime.Subtract(StartDateTime);
var duration =
    (int)durationTimeSpan.TotalHours + ":" +
    durationTimeSpan.Minutes.ToString("00") + ":" +
    durationTimeSpan.Seconds.ToString("00");

The "duration" string will be "48:28:30" as desired.

Dan Roberts
  • 2,244
  • 16
  • 31
1

I came up with a quick solution. But it is not the best one. I tested it only with your data. So it will be needed some improvements.

 var startDateTime = Convert.ToDateTime("2016-08-25 15:00:00");
 var endDateTime = Convert.ToDateTime("2016-08-27 15:28:30");
 String.Format("{0}:{1}:{2}", 
            Convert.ToInt32(Math.Round((endDateTime - startDateTime).TotalHours -0.5, MidpointRounding.ToEven) ),
            (endDateTime - startDateTime).Minutes,
            (endDateTime - startDateTime).Seconds);
dsmyrnaios
  • 325
  • 1
  • 4
  • 12
0

Or in a more fashionable way:

string duration = String.Format($"{(int)seconds.TotalHours}:{seconds.Minutes}:{seconds.Seconds}");

*Edit: And also more readable

uTeisT
  • 2,256
  • 14
  • 26
0

Quite simply:

DateTime startDateTime;
DateTime.TryParse("2016-08-25 15:00:00", out startDateTime);
DateTime endDateTime; 
DateTime.TryParse("2016-08-27 15:28:30", out endDateTime);
TimeSpan span = (endDateTime - startDateTime);

var durationString = string.Format("{0}:{1}:{2}", (int)span.TotalHours, span.Minutes, span.Seconds);