1

i got a string with a datetime value so i want to validate that the value is always with this format "yyyy-MM-ddTHH:mm:ss" how can i do that?

i have this code but it´s always throwing true.

  public Boolean validaFecha(string fecha)
    {
        DateTime dDate;
        Boolean resp = false;

        if (DateTime.TryParse(fecha, out dDate))
        {
            resp = true;
        }

        return resp;

    }
checo
  • 49
  • 1
  • 8

2 Answers2

2

You can use the DateTime.TryParseExact method and specify the format :

public static Boolean validaFecha(string fecha)    
{
    DateTime dDate;
    return DateTime.TryParseExact(fecha, "yyyy-MM-ddTHH:mm:ss", 
        CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate);
}

Example of use :

bool isValid = validaFecha("2015-01-24T12:15:54"); // Will be true
Fabien ESCOFFIER
  • 4,751
  • 1
  • 20
  • 32
-1

Use :

DateTime.TryParseExact Method (String, String, IFormatProvider)

[MSDN Date Time Try Parse Exact][1]
Avia
  • 1,829
  • 2
  • 14
  • 15