DateTime.parseExact does not work

1

I want to validate if a string is a date, the string I bring from a connection with an API and it comes in the following way

20181025

public static Boolean IsDate(string Expression)
    {
        Boolean resultado = false;
        try
        {
            DateTime isDate = DateTime.ParseExact(Expression, "dd/MM/yyyy", null);
            resultado = true;
        }
        catch
        {
            resultado = false;
        }
        return resultado;
    }

but even using the DateTime I can not parse it and the function returns false

    
asked by Ernesto Emmanuel Yah Lopez 23.10.2018 в 17:06
source

2 answers

4

The format you try to enter for the value 20181025 is "yyyyMMdd", but you are defining the format "dd / MM / yyyy", which is incorrect:

  DateTime isDate = DateTime.ParseExact(Expression, "dd/MM/yyyy", null);

If you want to use the format "yyyyMMdd", you must define it for the method DateTime.ParseExact () :

  DateTime isDate = DateTime.ParseExact(Expression, "yyyyMMdd", null);
    
answered by 23.10.2018 / 17:16
source
1

The date is 20181025 does not match the format you are indicating in DateTime.ParseExact , instead of "dd/MM/yyyy" test with "yyyyMMdd" .

    
answered by 23.10.2018 в 17:13