MudBlazor/MudBlazor · error · ConversionException

Not a valid date time

Error message

Not a valid date time

What it means

Thrown by TimeOnlyConverter.ConvertBack when the input is non-empty but TimeOnly.TryParseExact fails. ConversionException key is Converter_InvalidDateTime (a TODO notes TimeOnly does not yet have its own message). Note a latent quirk: when Format() is null the fallback is the culture's ShortDatePattern — a date pattern used to parse a time — which makes null Format very likely to throw. Empty input returns default(TimeOnly).

Source

Thrown at src/MudBlazor/Converters/DefaultConverter.TimeOnly.cs:34

        public string Convert(TimeOnly input) => input.ToString(format.Invoke(), culture.Invoke());

        public string? Convert(TimeOnly? input) => input?.ToString(format.Invoke(), culture.Invoke());

        public TimeOnly ConvertBack(string? input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return default;
            }

            var currentCulture = culture.Invoke();
            if (TimeOnly.TryParseExact(input, format.Invoke() ?? currentCulture.DateTimeFormat.ShortDatePattern, currentCulture, DateTimeStyles.None, out var result))
            {
                return result;
            }

            // TODO: Differentiate error message for TimeOnly
            throw new ConversionException(LanguageResource.Converter_InvalidDateTime);
        }

        TimeOnly? IReversibleConverter<TimeOnly?, string?>.ConvertBack(string? input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return null;
            }

            return ConvertBack(input);
        }
    }
}

View on GitHub (pinned to bdb3acd5dd)

Solutions

  1. Always set an explicit time Format (e.g. "HH:mm" or "h:mm tt") on the component/converter — never rely on the null fallback.
  2. Pre-validate with TimeOnly.TryParseExact(input, format, culture, DateTimeStyles.None, out _).
  3. Catch ConversionException and show the localized message.
  4. Use a MudTimePicker so the value round-trips through the same format.

Example fix

// before
var converter = new DefaultConverter<TimeOnly>(); // Format=null -> ShortDatePattern
var t = converter.ConvertBack("14:30"); // throws

// after
var converter = new DefaultConverter<TimeOnly> { Format = () => "HH:mm" };
Defensive patterns

Strategy: try-catch

Validate before calling

static bool CanParseTimeOnly(string? input, CultureInfo culture, string? format) =>
    string.IsNullOrEmpty(input) ||
    TimeOnly.TryParseExact(input, format ?? "HH:mm", culture, DateTimeStyles.None, out _);

Try / catch

try { var t = converter.ConvertBack(input); }
catch (ConversionException ex) { ReportUserError(ex.ErrorMessageKey, ex.ErrorMessageArgs); }

Prevention

When it happens

Trigger: ConvertBack("14:30:00") when Format is null (falls back to ShortDatePattern and fails); ConvertBack("2:30 PM") when the format is "HH:mm"; any time string not exactly matching the configured format.

Common situations: Forgetting to set a time Format on the converter/component so it falls back to a date pattern; 12-hour vs 24-hour mismatch; culture whose time patterns differ from what the user types.

Related errors


AI-assisted analysis of MudBlazor/MudBlazor@bdb3acd5dd (2026-08-13). Data as JSON: /api/errors/ea00c5753484e77b. Report an issue: GitHub.