Specified method is not supported which works fine when I execute in WindowsFormsApp

184 Views Asked by At

My intention is to check if the user provided date and time is valid. I have checked the below code in WindowsFormsApp and it worked well there. I am working with RPA Automation Anywhere where I am creating a .Net DLL to validate date and time of inputs. When I pass the date time parameters from Automation Anywhere tool, I am getting an error "Specified method is not supported."

I am new to C#, kindly advise how I can modify the below code to solve the issue.

strDate value passed as '06/06/2020' strTime value passed as '14:45:04' from RPA tool Automation Anywhere

strDate = strDate.ToString();
strTime = strTime.ToString();

        try
        {
            //Date format checks
            string[] strDateFormats = { "mm/dd/yyyy", "m/d/yyyy", "mm/d/yyyy", "m/dd/yyyy", "mm/dd/yy", "m/d/yy", "mm/d/yy", "m/dd/yy" };

            if (DateTime.TryParseExact(strDate, strDateFormats,  CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out DateTime dt1))
                //if (DateTime.TryParseExact(strDate, strDateFormats, new CultureInfo("en-US"), DateTimeStyles.None, out DateTime dt1))
                {
                strDateReturn = dt1.ToString("mm/dd/yyyy");
                }
            //Provided Date is invalid, set it with empty value
            else
            {
                strDateReturn = string.Empty;
            }

            //Time format checks
            string[] strTimeFormats = { "h\\:m\\:s", "H\\:m\\:s"};
            //string[] strTimeFormats = { "hh\\:mm\\:ss", "hh\\:m\\:s", "hh\\:mm\\:s", "hh\\:m\\:ss", "h\\:mm\\:ss", "h\\:m\\:s", "h\\:m\\:ss", "h\\:mm\\:s", "HH\\:mm\\:ss", "HH\\:m\\:s", "HH\\:mm\\:s", "HH\\:m\\:ss", "H\\:mm\\:ss", "H\\:m\\:s", "H\\:m\\:ss", "H\\:mm\\:s" };
            bool res = DateTime.TryParse(strTime, out DateTime dt3);

            //If res==True, the provided input is in AM/PM format and converting the same in 24 hrs format
            if (res == true)
            {
                strTime = dt3.ToString("HH:mm:ss");
                strTimeReturn = strTime;
            }

            //If res==False, the provided input is in 24 hrs format but not sure if the provided time is valid
            else
            {
                if (TimeSpan.TryParseExact(strTime, strTimeFormats, CultureInfo.InvariantCulture, TimeSpanStyles.None, out TimeSpan dt2))
                //if (TimeSpan.TryParse(strTime, CultureInfo.InvariantCulture, out TimeSpan dt2))
                    //if (TimeSpan.TryParseExact(strTime, strTimeFormats, new CultureInfo("en-US"), out TimeSpan dt2))
                    {
                    strTimeReturn = dt2.ToString("HH:mm:ss");
                }
                //If invalid time is provided set it as empty
                else
                {
                    strTimeReturn = string.Empty;
                }
            }

        }

`

I get this error by running it Error with DateTime.TryParseExact/TimeSpan.TryParseExact

0

There are 0 best solutions below