Time formatter
Format a TimeSpan, DateTime, or DateTimeOffset as human-readable text.
Read time 3 minutesLast updated 5 days ago
Format a , , or as human-readable text.
TimeSpanDateTimeDateTimeOffsetThe Time formatter turns a duration into readable text, such as . It accepts a TimeSpan, or a DateTime or DateTimeOffset measured against the current time. Apply it explicitly with the name .
1 day 6 hourstimeAnatomy of the time formatter syntax.
The format part holds the options, separated by spaces. The optional parentheses hold a culture name, for example . When no culture is given, the formatter uses the culture passed to . If no culture is provided, it uses the active culture, and falls back to English.
{0:time(en):abbr}Smart.Format// "1 day 6 hours" Smart.Format("{0:time:less d h}", new TimeSpan(1, 6, 0, 0)); // "1 day 1 hour 1 minute 1 second" (active culture en) Smart.Format("{0:time:}", new TimeSpan(1, 1, 1, 1, 1));
Combine options by separating them with spaces. The following options are available:
Option | Description |
|---|---|
| Include weeks in the range. |
| Include days in the range. |
| Include hours in the range. |
| Include minutes in the range. |
| Include seconds in the range. |
| Include milliseconds in the range. |
| Display the highest non-zero value in the range. For example |
| Display all non-zero values in the range. For example |
| Display the highest non-zero value and all lesser values in the range. For example |
| Display all values in the range. For example |
| Abbreviate units, for example |
| Don't abbreviate units, for example |
| Display "less than 1 (unit)" when the value is smaller than the minimum range. |
| Display "0 (units)" when the value is smaller than the minimum range. |
Use two range options to set the minimum and maximum unit. For example, shows days only, while shows hours down to seconds.
dh sWhen no options are given, the defaults are .
noabbr less auto seconds daysOutput is localized through the resolved culture, for example , , or . The fallback culture is English. Set to change the fallback, or set it to an empty string to disable the fallback.
endefrTimeFormatter.FallbackLanguageThe Time formatter can't handle nested formats. Applying it to an unsupported type throws a .
FormatExceptionSmart String | Argument | Result |
|---|---|---|
| | 451d 6h 22m 5s |
| | 451 days 6 hours 22 minutes 5 seconds |
| | less than 1 week |
| | 1 day 6 hours |
A or argument is formatted as the duration between it and the current time. A is formatted directly.
DateTimeDateTimeOffsetTimeSpan