Skip to content

TimeSpan _ TimeFormatter

axunonb edited this page Mar 30, 2022 · 3 revisions

The TimeFormatter is used to format TimeSpan, DateTime and DateTimeOffset values.

Syntax Details

{ Value : time : format }

Value formatter name format
TimeSpan "time" or implicit format to use

Depreciated:
"{0:time(abbr hours noless)}"
This notation - using formats as formatter options - was allowed in Smart.Format v2.x, but is now depreciated. It is still detected and working, as long as the format part is left empty. It will be removed in a future version.

Recommended:
"{0:time:abbr hours noless:}"
With the language as a format option:
"{0:time(en):abbr hours noless:}"

Configuration:

string Name: default is time
The name to use a named formatter

DefaultFormatOptions: default is TimeSpanUtility.DefaultFormatOptions
which is
TimeSpanFormatOptions.AbbreviateOff | TimeSpanFormatOptions.LessThan | TimeSpanFormatOptions.TruncateAuto | TimeSpanFormatOptions.RangeSeconds | TimeSpanFormatOptions.RangeDays

FallbackLanguage: default is "en"
Fallback language that is used if no supported language was found.

Supported Languages

TimeFormatter includes French, Spanish, Portuguese, Italian and German as languages besides English out-of-the-box.

Custom languages can easily be added to CommonLanguagesTimeTextInfo. Custom languages override built-in definitions.

TimeSpanFormatOptions

Option Description
abbr (0x1) Abbreviates units
Example: "1d 2h 3m 4s 5ms"
noabbr (0x2) Does not abbreviate units
Example: "1 day 2 hours 3 minutes 4 seconds 5
less (0x4) Displays "less than 1 (unit)" when the TimeSpan is smaller than the minimum range.
noless (0x8) Displays "0 (units)" when the TimeSpan is smaller than the minimum range.
short (0x10) Displays the highest non-zero value within the range.
auto (0x20) Displays all non-zero values within the range.
Example: "00.23:00:59.000" = "23 hours 59 minutes
fill (0x40) Displays the highest non-zero value and all lesser values within the range.
Example: "00.23:00:59.000" = "23 hours 0 minutes 59 seconds 0 milliseconds"
full (0x80) Displays all values within the range.
Example: "00.23:00:59.000" = "0 days 23 hours 0 minutes 59 seconds 0 milliseconds"
ms, milliseconds, millisecond (0x100) Determines the range of units to display. You may combine two values to form the minimum and maximum for the range.
Example: (RangeMinutes) defines a range of Minutes only; (RangeHours (pipe) RangeSeconds) defines a range of Hours to Seconds.
s, seconds, second (0x200) Determines the range of units to display. See RangeMilliSeconds for more.
m, minutes, minute (0x400) Determines the range of units to display. See RangeMilliSeconds for more.
h, hours, hour (0x800) Determines the range of units to display. See RangeMilliSeconds for more.
d, days, day (0x1000) Determines the range of units to display. See RangeMilliSeconds for more.
w, weeks, week (0x2000) Determines the range of units to display. See RangeMilliSeconds for more.

Examples

var args = new object[] {
    TimeSpan.Zero,
    new TimeSpan(1,1,1,1,1),
    new TimeSpan(0,2,0,2,0),
    new TimeSpan(3,0,0,3,0),
    new TimeSpan(0,0,0,0,4),
    new TimeSpan(5,0,0,0,0)
};

// Register the TimeFormatter extension
Smart.Default.AddExtensions(new TimeFormatter());

Smart.Format("{0:time(en):noless}", args);
// Outputs: "0 seconds"

Smart.Format("{1:time(en):hours}", args);
// Outputs: "25 hours"

Smart.Format("{1:time(en):hours minutes}", args);
// Outputs: "25 hours 1 minute"

Smart.Format("{2:time(en):days milliseconds}", args);
// Outputs: "2 hours 2 seconds"

Smart.Format("{2:time(en):days milliseconds auto}", args);
// Outputs: "2 hours 2 seconds"

Smart.Format("{2:time(en):days milliseconds short}", args);
// Outputs: "2 hours"

Smart.Format("{2:time(en):days milliseconds fill}", args);
// Outputs: "2 hours 0 minutes 2 seconds 0 milliseconds"

Smart.Format("{2:time(en):days milliseconds full}", args);
// Outputs: "0 days 2 hours 0 minutes 2 seconds 0 milliseconds"

Smart.Format( "{3:time(en):abbr}", args);
// Outputs:  "3d 3s"
Clone this wiki locally