Howdy

I have a timespan that contain in excess of 24 hours. For the example lets use a value of 1 day 6 hours and 32 minutes.

I would like to get this into a string that displays "30:32".

Have so far tried something along the lines of string.Format("{0}:{1}", TimeSpan.TotalHours, TimeSpan.Minutes) however this gives me a scenario where it'll display 13:0 rather than 13:00 which I also failed to resolve.

Dani AI

Generated

the key is that TimeSpan formatting tokens like hh wrap at 24, so timeSpan.ToString("hh:mm") will never show 30 hours. You need the total hours, not the hour component. Also, your "13:0" issue is just missing numeric formatting; use :00 to pad with a leading zero.

If you want to truncate seconds (common for duration readouts), this is the simplest form:

var ts = new TimeSpan(1, 6, 32, 0); // 1 day, 6 hours, 32 minutes
string s = $"{(long)ts.TotalHours:00}:{ts.Minutes:00}"; // "30:32"

If you prefer rounding to the nearest minute (so 59+ seconds bumps the minute and can carry into hours), compute from total minutes and split:

var ts = new TimeSpan(1, 6, 32, 45);
long totalMinutes = (long)Math.Round(ts.TotalMinutes, MidpointRounding.AwayFromZero);
long hours = totalMinutes / 60;
long minutes = totalMinutes % 60;
string s = $"{hours:00}:{minutes:00}";

Both approaches avoid the 24-hour wrap. ’s subtract-then-format method works too; the total-minutes route is just a bit shorter and naturally handles any carry from rounding. One small gotcha: make sure you are calling instance properties (e.g., mySpan.TotalHours), not the TimeSpan type itself. And if your spans can be negative, capture the sign first and format the absolute value, then reapply the sign:

var sign = ts < TimeSpan.Zero ? "-" : "";
ts = ts.Duration();
string s = $"{sign}{(long)ts.TotalHours:00}:{ts.Minutes:00}";

That should give you the exact "30:32" you’re after without extra parsing, and it scales cleanly for durations well beyond a day.

Recommended Answers

All 2 Replies

The thing is, TimeSpan for 24 and over hour discussions are pretty well down. I used this link to find the priors.

Here is a one solution

    TimeSpan timeSpan;
    string timeStr;
    int totalHours;
    int totalMinutes;

    // 1 day, 6 hrs, 32 mins, 0 secs, 0 msecs
    timeSpan = new TimeSpan(1, 6, 32, 0, 0); 
    // Get hours and minutes
    // TotalHours is of type double, cast it to integer (truncate to integer)
    totalHours = (int)timeSpan.TotalHours;
    // Remove totalHours hours from the timeSpan to avoid rounding error
    timeSpan = timeSpan.Subtract(new TimeSpan(totalHours, 0, 0)); // hours, 0 mins, 0 secs
    // TotalMinutes is of type double, cast it to integer (truncate to integer)
    totalMinutes = (int)timeSpan.TotalMinutes;
    // Convert to a string
    timeStr = string.Format("{0:d2}:{1:d2}", totalHours, totalMinutes);
    // Display result
    MessageBox.Show(timeStr);

Notice that I created timespan with explicit seconds and milliseconds. You may have got this one wrong. I subtracted (total) hours from the timespan. After that it has only minutes (and seconds and mseconds) left. To format with two digits you can use for example "d2".

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.