TimeFormatter
Formats time spans as human-readable text, such as 1 day 2 hours.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Class
- Namespace: Unity.SmartStrings.Extensions
- Assembly: UnityEngine.SmartStringsModule
- Inherits from: FormatterBase
- Implements: IFormatter
public class TimeFormatter : FormatterBase, IFormatter
Remarks
Call the formatter by name, as in . It accepts TimeSpan, DateTime and DateTimeOffset values. For a DateTime or DateTimeOffset, it shows the time between that value and now. Words in the format text, such as or , set TimeSpanFormatOptions for that placeholder. Without a nested format, the formatter joins the unit texts with spaces. When the format holds a nested placeholder, the formatter passes the list of unit texts to it instead, so a nested list format can choose the separators, as in . Only the words before the nested placeholder set options.
{0:time:}shortnoless{0:time:abbr {:list:|, | and }}Additional Resources: TimeSpanFormatOptions, ListFormatter
Examples
using System;using System.Globalization;using Unity.SmartStrings;using Unity.SmartStrings.Extensions;using UnityEngine;public static class TimeFormatterExample{ public static void FormatDuration() { var smart = Smart.CreateDefaultSmartFormat(); smart.AddExtensions(new TimeFormatter()); var english = CultureInfo.GetCultureInfo("en"); var duration = new TimeSpan(1, 1, 1, 1); Debug.Log(smart.Format(english, "{0:time:}", duration)); // 1 day 1 hour 1 minute 1 second Debug.Log(smart.Format(english, "{0:time: {:list:|, | and }}", duration)); // 1 day, 1 hour, 1 minute and 1 second }}
Constructors
Constructor | Description |
|---|---|
| TimeFormatter() | Initializes the extension with a default TimeTextInfo. |
Properties
Property | Description |
|---|---|
| DefaultFormatOptions | The options that control how time values are formatted. |
| FallbackLanguage | The fallback language used when no supported language is found. Default is "en". If no fallback language shall be used, set it to Empty. |