Humanizr/Humanizer · error · FormatException

Value is not a supported TimeSpan format.

Error message

Value is not a supported TimeSpan format.

What it means

Thrown by DehumanizeTimeSpan when the input string cannot be parsed as either a standard invariant TimeSpan (e.g. "1:30:00") or compact duration tokens using ms, s, m, h, d, w (e.g. "1h30m"). The method first tries TryDehumanizeTimeSpan and throws FormatException when it returns false.

Source

Thrown at src/Humanizer/TimeSpanDehumanizeExtensions.cs:33

    /// </summary>
    /// <param name="input">The duration text to parse.</param>
    /// <returns>The parsed duration.</returns>
    /// <exception cref="ArgumentNullException">Thrown when <paramref name="input"/> is null.</exception>
    /// <exception cref="FormatException">Thrown when <paramref name="input"/> is not a supported duration.</exception>
    /// <remarks>
    /// Compact tokens are culture-invariant, may be separated by whitespace, and may have one leading sign.
    /// Units may repeat and appear in any order; their values are added. Each token must resolve to whole ticks.
    /// A week is seven days.
    /// Colon-separated values use the standard invariant <see cref="TimeSpan"/> interpretation.
    /// </remarks>
    public static TimeSpan DehumanizeTimeSpan(this string input)
    {
        ArgumentNullException.ThrowIfNull(input);

        if (TryDehumanizeTimeSpan(input, out var result))
            return result;

        throw new FormatException("Value is not a supported TimeSpan format.");
    }

    /// <summary>
    /// Tries to parse a standard invariant <see cref="TimeSpan"/> value or compact duration tokens using
    /// <c>ms</c>, <c>s</c>, <c>m</c>, <c>h</c>, <c>d</c>, and <c>w</c>.
    /// </summary>
    /// <param name="input">The duration text to parse.</param>
    /// <param name="result">The parsed duration, or <see cref="TimeSpan.Zero"/> when parsing fails.</param>
    /// <returns><see langword="true"/> when parsing succeeds; otherwise, <see langword="false"/>.</returns>
    /// <remarks>
    /// Compact tokens are culture-invariant, may be separated by whitespace, and may have one leading sign.
    /// Units may repeat and appear in any order; their values are added. Each token must resolve to whole ticks.
    /// A week is seven days.
    /// Colon-separated values use the standard invariant <see cref="TimeSpan"/> interpretation.
    /// </remarks>
    public static bool TryDehumanizeTimeSpan(this string? input, out TimeSpan result)
    {
        result = default;

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Use TryDehumanizeTimeSpan instead to get a boolean result without throwing.
  2. Validate the input contains only supported tokens (ms, s, m, h, d, w) or a colon-separated TimeSpan.
  3. Provide a fallback TimeSpan when parsing fails.

Example fix

// before
var ts = input.DehumanizeTimeSpan();

// after
if (!input.TryDehumanizeTimeSpan(out var ts))
    ts = TimeSpan.Zero;
Defensive patterns

Strategy: validation

Validate before calling

static bool TrySafeDehumanize(string input, out TimeSpan result) =>
    input.TryDehumanizeTimeSpan(out result);

Try / catch

if (!input.TryDehumanizeTimeSpan(out var ts))
{
    // handle invalid input
}

Prevention

When it happens

Trigger: Calling "hello".DehumanizeTimeSpan(), "1y".DehumanizeTimeSpan() (year is not a token), or "".DehumanizeTimeSpan(). Also fires for tokens that don't resolve to whole ticks.

Common situations: Parsing localized duration words (e.g. "one hour") instead of compact tokens. Using unsupported unit abbreviations. Feeding user-entered free-text into DehumanizeTimeSpan without validation.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/72ce5d644c54a45a. Report an issue: GitHub.