Format TimeSpan in C#

The TimeSpan class in C# represents a time interval and provides several formatting options to display time in different formats. You can format a TimeSpan using standard format strings, custom format strings, or the ToString() method with format specifiers.

Syntax

Following is the syntax for formatting a TimeSpan using custom format strings −

timeSpan.ToString("format_string")

Following is the syntax for formatting using composite formatting −

string.Format("{0:format_string}", timeSpan)

Using Standard Format Strings

TimeSpan supports standard format strings like "c" (constant format), "g" (general short), and "G" (general long) −

using System;

public class Demo {
    public static void Main() {
        TimeSpan ts = new TimeSpan(9, 15, 30);
        
        Console.WriteLine("Constant format (c): " + ts.ToString("c"));
        Console.WriteLine("General short (g): " + ts.ToString("g"));
        Console.WriteLine("General long (G): " + ts.ToString("G"));
    }
}

The output of the above code is −

Constant format (c): 09:15:30
General short (g): 9:15:30
General long (G): 0:09:15:30

Using Custom Format Strings

Custom format strings allow you to specify exactly how the TimeSpan should be displayed using format specifiers like hh, mm, ss −

using System;

public class Demo {
    public static void Main() {
        TimeSpan ts = new TimeSpan(2, 9, 15, 30);
        
        Console.WriteLine("hh:mm:ss format: " + ts.ToString(@"hh\:mm\:ss"));
        Console.WriteLine("d days, h hours: " + ts.ToString(@"d\d\ h\h"));
        Console.WriteLine("Custom format: " + ts.ToString(@"dd\.hh\:mm\:ss"));
        Console.WriteLine("Hours and minutes: " + ts.ToString(@"h\:mm"));
    }
}

The output of the above code is −

hh:mm:ss format: 09:15:30
d days, h hours: 2d 9h
Custom format: 02.09:15:30
Hours and minutes: 57:15

Using Composite Formatting

You can also use composite formatting with Console.WriteLine or string.Format to format TimeSpan values −

using System;

public class Demo {
    public static void Main() {
        TimeSpan ts1 = new TimeSpan(1, 30, 45);
        TimeSpan ts2 = new TimeSpan(0, 0, 5, 30);
        
        Console.WriteLine("Time 1: {0:hh\:mm\:ss}", ts1);
        Console.WriteLine("Time 2: {0:mm\:ss}", ts2);
        Console.WriteLine("Formatted: {0:g} and {1:c}", ts1, ts2);
        
        string formatted = string.Format("Duration: {0:h\h\ m\m\ s\s}", ts1);
        Console.WriteLine(formatted);
    }
}

The output of the above code is −

Time 1: 01:30:45
Time 2: 05:30
Formatted: 1:30:45 and 00:05:30
Duration: 1h 30m 45s

Common Format Specifiers

Format Specifier Description Example Output
d Days (1-digit minimum) 2
dd Days (2-digit minimum) 02
h Hours (1-digit minimum) 9
hh Hours (2-digit minimum) 09
m Minutes (1-digit minimum) 5
mm Minutes (2-digit minimum) 05
s Seconds (1-digit minimum) 7
ss Seconds (2-digit minimum) 07

Conclusion

TimeSpan formatting in C# can be achieved using standard format strings like "c" and "g", or custom format strings with specifiers like hh, mm, and ss. Use escape sequences with backslashes when including literal characters like colons in custom format strings.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements