Home > Back-end >  C# Timespan in hh:mm:ss With Days Included In Hours
C# Timespan in hh:mm:ss With Days Included In Hours

Time:09-28

I have a sample TimeSpan object like this:

{9.04:00:00}
    Days: 9
    Hours: 4
    Milliseconds: 0
    Minutes: 0
    Seconds: 0
    Ticks: 7920000000000
    TotalDays: 9.1666666666666661
    TotalHours: 220
    TotalMilliseconds: 792000000
    TotalMinutes: 13200
    TotalSeconds: 792000

When I run .ToString() on it, it is returned in this format: "9.04:00:00". However, I would like to have it in hh:mm:ss format only.

The expected output should be "220:00:00". How would I achieve that?

CodePudding user response:

There is no way to do this with format strings. They don't support triple digit hours. You'll have to write a method.

public static string ToHourString(this TimeSpan t) =>
    $"{(t.TotalHours >= 1 ? t.TotalHours : 0):00}:{t:mm\\:ss}";   

Output of TimeSpan.FromHours(220).ToHourString():

220:00:00

Output of TimeSpan.FromMinutes(44).ToHourString():

0:44:00

(Thanks Tom!)

CodePudding user response:

You can use thouse examples:

            TimeSpan ts = new TimeSpan(9, 4, 0, 0);
        MessageBox.Show(String.Format("{0}:{1}:{2}", (int)ts.TotalHours, ts.Minutes,ts.Seconds));
        MessageBox.Show(String.Format("{0:00}:{1:00}:{2:00}",
                                       (int)ts.TotalHours,
                                       ts.Minutes,
                                       ts.Seconds));  

First example:

enter image description here

Second:

enter image description here

CodePudding user response:

Got another way of doing it with the help of Victor's answer:

public static string ToHourFormat(this TimeSpan @this)
{
    string answer = string.Format("{0:D2}:{1:D2}:{2:D2}",
        @this.Hours   @this.Days * 24,
        @this.Minutes,
        @this.Seconds);

    return answer;
}

CodePudding user response:

I don't believe that there is a way to do what you want directly.TimeSpan format strings don't support that.

You could write a customer IFormatProvider and the corresponding ICustomFormatter

[Doing it right is more difficult than you might think.] . . . Or you could just write a simple extension method along these lines:

using System;

namespace Example
{
  public class Program
  {
    
    public static void Main()
    {
      TimeSpan ts = new TimeSpan(1,2,3,4,5); // 1d2h3m4s5ms
      Console.WriteLine( ts.ToHMS() );
    }
  
  }
  
  public static class TimeSpanExtensions
  {
    
    public static string ToHMS( this TimeSpan ts )
    {
      long hh  = 24 * ts.Days   ts.Hours;
      int  mm  = ts.Minutes;
      int  ss  = ts.Seconds;
      
      string s = string.Format( "{0:00}:{1:00}:{2:00}", hh, mm, ss );
      return s;     
    }
    
  }

}

If you need to support localization, though, the custom formatter is probably where you need to go.

CodePudding user response:

Here's a simple function for returning a TimeSpan, returning a string with the TimeSpan formatted in hh:mm:ss. Bool is optional (to clarify).

private string getTime(TimeSpan ts, bool secondZero = true)
    {
        if (!secondZero)
        {
            return String.Format("{0}:{1}:{2}", (int)ts.TotalHours, ts.Minutes, ts.Seconds);
        }
        else
        {
            return String.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, ts.Minutes, ts.Seconds);
        }
    }
  •  Tags:  
  • c#
  • Related