TimeSpan format string tester

Type a duration and a format to see what TimeSpan.ToString(format) returns.

Examples

Format1.02:03:04.567
c1.02:03:04.5670000
g1:2:03:04.567
G1:02:03:04.5670000
hh\:mm\:ss02:03:04
hh\:mm02:03
d\.hh\:mm\:ss1.02:03:04
mm\:ss\.fff03:04.567
hh':'mm02:03
d'd 'h'h 'm'm'1d 2h 3m
%d1
dd\.hh\:mm01.02:03

Formatting dates? Try the DateTime format tester.

Questions

Why does "hh:mm" throw a FormatException?

In TimeSpan custom formats every character that isn’t a format letter must be escaped or quoted. Write hh\:mm (in C#: @"hh\:mm" or "hh\\:mm") or hh':'mm.

How do I show a minus sign?

Standard formats (c, g, G) include it. Custom formats don’t: add a literal \- yourself when the value is negative.

Are hours above 23 possible?

Not with hh: hours are 0–23 and whole days go in d. For total hours, format (int)span.TotalHours separately.

Which .NET version is this checked against?

.NET 10.0.11: 396 test cases give the same text as TimeSpan.ToString.

More tools

Runs in your browser: nothing you paste is sent anywhere. Not affiliated with Microsoft, the .NET Foundation or the library authors.