# TimeFormatter

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

## Definition

* **Type:** Class
* **Namespace:** [Unity.SmartStrings.Extensions](/engine/7000.0/script-reference/unity/smartstrings/extensions.md)
* **Assembly:** UnityEngine.SmartStringsModule
* **Inherits from:** [FormatterBase](/engine/7000.0/script-reference/unity/smartstrings/core/extensions/formatterbase.md)
* **Implements:** [IFormatter](/engine/7000.0/script-reference/unity/smartstrings/core/extensions/iformatter.md)

```csharp
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](/engine/7000.0/script-reference/unity/smartstrings/extensions/time/utilities/timespanformatoptions.md) 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](/engine/7000.0/script-reference/unity/smartstrings/extensions/time/utilities/timespanformatoptions.md), [ListFormatter](/engine/7000.0/script-reference/unity/smartstrings/extensions/listformatter.md)

## Examples

```csharp
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()](/engine/7000.0/script-reference/unity/smartstrings/extensions/timeformatter/ctor.md) | Initializes the extension with a default TimeTextInfo. |

## Properties

| Property                                                                                                                    | Description                                                                                                                              |
| --------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| [DefaultFormatOptions](/engine/7000.0/script-reference/unity/smartstrings/extensions/timeformatter/defaultformatoptions.md) | The options that control how time values are formatted.                                                                                  |
| [FallbackLanguage](/engine/7000.0/script-reference/unity/smartstrings/extensions/timeformatter/fallbacklanguage.md)         | The fallback language used when no supported language is found. Default is "en". If no fallback language shall be used, set it to Empty. |

## Inheritance

**Inherited Members:**

* [FormatterBase.DefaultName()](/engine/7000.0/script-reference/unity/smartstrings/core/extensions/formatterbase/defaultname.md)
