Why can not Convert.ToDateTime convert string for the month of March? ("01-Mar-2017")

4

From the reading of an Excel spreadsheet I receive the following information:

  • "01-Mar-2017"
  • "Aug 1, 2017"
  • "01-Jun-2017"
  • "0"
  • To validate this data I do:

    (I configure the cultural info to transform the date, I already tried "en-US", "es-ES", "es" and "es-AR")

    System.Globalization.CultureInfo cInfo = new System.Globalization.CultureInfo("es-AR");
    try
    {
    DateTime dt = Convert.ToDateTime(sFecha, cInfo); //sFecha es alguno de los valores anteriormente mencionados
    }
    catch
    {
    //informo que el dato no respeta el formato de fecha
    }
    

    With the System.Globalization.CultureInfo "es-AR" All the cases work for item 1. "01-Mar-2017" (I do not understand why it should work), the dates of March do not convert them, but if I change the System.Globalization.CultureInfo to the "en-US" if it converts the dates of March but when an August date arrives it obviously does not walk (August = August in English).

    Someone could share some way in which you can validate these dates using the DateTime (I can verify the format of the chain and check it by analyzing each character, but I suppose there should be a kinder way to do it)

    DateTime.TryParse
    

    DateTime.TryParse does not work, it converts "0" to a date.

        
    asked by NorbyAriel 24.10.2017 в 19:44
    source

    2 answers

    5

    It's a bit weird that it does not work the way you are doing, the option that could help you is to define in the culture info the abbreviation of the months in a personalized way, I leave an example that I hope will solve your problem

    var x = new System.Globalization.DateTimeFormatInfo();
    x.FullDateTimePattern = "dd/M/yyy";
    x.AbbreviatedMonthNames = new string[] { "ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic","" };
    System.Globalization.CultureInfo cInfo = new System.Globalization.CultureInfo("es-AR");
    cInfo.DateTimeFormat = x;
    
            try
            {
                DateTime dt = Convert.ToDateTime(sFecha.ToUpper(), cInfo); //sFecha es alguno de los valores anteriormente mencionados
            }
            catch (Exception ex)
            {
                //informo que el dato no respeta el formato de fecha
            }
    
        
    answered by 25.10.2017 / 09:18
    source
    3

    The problem is that you do not pass the current format of the date you are converting.

    I hope this helps you. Greetings

    static void Main(string[] args)
    {
        string[] dateValues = { "01-mar-2017" ,"01-ago-2017", "01-jun-2017" ,"0"};
    
        var format = "dd-MMM-yyyy";
    
        foreach (var dateValue in dateValues)
        {
            ValidarFechas(dateValue, format);
        }
    
        Console.Read();
    }
    
    
    public static void ValidarFechas(string sFecha, string format)
    {
        if (DateTime.TryParseExact(sFecha, format, null, DateTimeStyles.None, out var parsedDate))
            Console.WriteLine("convertido '{0}' a {1:d}.", sFecha, parsedDate);
        else
            Console.WriteLine("no se puede convertir '{0}' a fecha.", sFecha);
    }
    

    Console output:

    convertido '01-mar-2017' a 01/03/2017.
    convertido '01-ago-2017' a 01/08/2017.
    convertido '01-jun-2017' a 01/06/2017.
    no se puede convertir '0' a fecha.
    
        
    answered by 25.10.2017 в 08:48