TimeSpan format string tester
Type a duration and a format to see what TimeSpan.ToString(format) returns.
Examples
| Format | 1.02:03:04.567 |
|---|---|
| c | 1.02:03:04.5670000 |
| g | 1:2:03:04.567 |
| G | 1:02:03:04.5670000 |
| hh\:mm\:ss | 02:03:04 |
| hh\:mm | 02:03 |
| d\.hh\:mm\:ss | 1.02:03:04 |
| mm\:ss\.fff | 03:04.567 |
| hh':'mm | 02:03 |
| d'd 'h'h 'm'm' | 1d 2h 3m |
| %d | 1 |
| dd\.hh\:mm | 01.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
- Cron expression tester
- appsettings.json ⇄ environment variables
- SQL Server connection string builder
- PostgreSQL connection string builder
- DateTime format string tester
- JSON to C# records
Runs in your browser: nothing you paste is sent anywhere. Not affiliated with Microsoft, the .NET Foundation or the library authors.