Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


TimeFormatter

Formats time spans as human-readable text, such as 1 day 2 hours.
Read time 1 minuteLast updated 5 days ago

Definition

public class TimeFormatter : FormatterBase, IFormatter

Remarks

Call the formatter by name, as in
{0:time:}
. 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
short
or
noless
, 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
{0:time:abbr {:list:|, | and }}
. Only the words before the nested placeholder set options.
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

DefaultFormatOptionsThe options that control how time values are formatted.
FallbackLanguageThe fallback language used when no supported language is found. Default is "en". If no fallback language shall be used, set it to Empty.

Inheritance