dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidDataTypeOfParameter, " DateTime or…

Error message

SR.Format(SR.InvalidDataTypeOfParameter, " DateTime or string ")

What it means

The WindowsCalendar helper that converts a value into a Win32 SYSTEMTIME accepts only DateTime or string inputs; if 'val' is neither (and string parsing fails to yield a valid date), it throws ArgumentException(SR.Format(SR.InvalidDataTypeOfParameter, " DateTime or string ")) with parameter name 'val'.

Solutions

  1. Convert the value to DateTime (or an invariant-format date string) before passing it.
  2. Ensure strings parse with DateTime.Parse(value, CultureInfo.InvariantCulture).
  3. Catch ArgumentException and inspect val's runtime type to route the conversion correctly.

Example fix

// before
calendar.SetValue(dateTicks); // long
// after
calendar.SetValue(new DateTime(dateTicks));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(val is DateTime || (val is string s && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))))
    throw new ArgumentException("val must be DateTime or invariant date string", nameof(val));

Type guard

bool IsValidDateValue(object val) => val is DateTime || (val is string s && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out _));

Try / catch

try { calendar.SetValue(val); }
catch (ArgumentException ex) when (ex.ParamName == "val") { val = ConvertToDateTime(val); retry(); }

Prevention

When it happens

Trigger: Calling the calendar's value-conversion helper (used when setting the selected date) with a non-DateTime, non-string object such as int, long, or null.

Common situations: Passing numeric date representations (ticks, file times) or binding values of unexpected runtime types; strings that are not culture-invariant-parseable dates also funnel here.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/88a27d36c6dcdc33. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/UnsupportedAutomationProxies/WindowsCalendar.cs:1140

            if (val is DateTime)
            {
                systemTime = CreateSystemTimeFromDateTime((DateTime)val);
                fValid = true;
            }
            // PerSharp/PreFast will flag this as warning 6507/56507:
            // Prefer 'string.IsNullOrEmpty(valString)' over checks for null and/or emptiness.
            // Null and Empty string mean different things here.
#pragma warning suppress 6507
            else if (valString != null)
            {
                systemTime = CreateSystemTimeFromDateTime(
                                System.DateTime.Parse(valString, CultureInfo.InvariantCulture));
                fValid = true;
            }

            if (!fValid)
            {
                throw new ArgumentException (
                    SR.Format(SR.InvalidDataTypeOfParameter, " DateTime or string "), "val");
            }

            return systemTime;
        }

        private int CalendarIndexFromPoint (int x, int y)
        {
            NativeMethods.Win32Rect rcMonth;
            CalcPositions (0, out rcMonth);

            NativeMethods.Win32Rect rc = new NativeMethods.Win32Rect ();
            NativeMethods.Win32Rect rcSingleMonth = new NativeMethods.Win32Rect ();

            if (!Misc.GetClientRectInScreenCoordinates(_hwnd, ref rc))
            {
                return -1;
            }

View on GitHub (pinned to 81131a70a4)