Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

TimeFormatter

class in Unity.SmartStrings.Extensions

/

Inherits from:Unity.SmartStrings.Core.Extensions.FormatterBase

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Formats time spans as human-readable text, such as 1 day 2 hours.

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

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 } }

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 String.Empty.

Constructors

Constructor Description
TimeFormatter Initializes the extension with a default Unity.SmartStrings.Extensions.Time.Utilities.TimeTextInfo.

Inherited Members

Properties

PropertyDescription
DefaultName Default name to use when FormatterBase.Name is null.