-
-
Notifications
You must be signed in to change notification settings - Fork 107
TimeSpan _ TimeFormatter
The TimeFormatter
is used to format TimeSpan
, DateTime
and DateTimeOffset
values.
{ 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:}"
string Name
: default istime
The name to use a named formatterDefaultFormatOptions: 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.
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.
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. |
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"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License