Error when converting date with 201801 format?

1

I have the following method in C #, which transforms the dates of this format: 201801 to this format: 01/01/2018 - 15/01/2018 which divides the month into 15 days, but the detail here is that when I put 201824 I get the following error:

  

String was not recognized as a valid DateTime.

Example of using the method is:

y = Quincena_Fecha("201824");

I guess it's a mistake because it goes beyond the days of the year, I'm new to this, so I'd like you to support me with this.

It should be noted that the 201824 date format means that it is divided as follows:

2018: year 24: fortnight in progress

Which would have to transform it into the following format: 01/12/2018 - 12/15/2018 but it sends me the error that I already mentioned in the image.

public string Quincena_Fecha(string text)
{
    int Año;
    int.TryParse(text.Substring(0, 4), out Año);
    int Mes;
    int Quincena;
    if (int.TryParse(text.Substring(4, 2), out Quincena))
    {
        double Valor = ((double)Quincena / 2);

        Mes = System.Convert.ToInt32((Quincena + 1) / 2);

        if (Valor == System.Convert.ToInt32(Quincena / 2))
        {
            return "16/" + Mes.ToString("00") + "/" + Año + " - " + Convert.ToDateTime("01/" + (Mes + 1).ToString("00") + "/" + Año).AddDays(-1).ToString("dd") + "/" + Mes.ToString("00") + "/" + Año;
        }
        else
        {
            return "01/" + Mes.ToString("00") + "/" + Año + " - " + "15/" + Mes.ToString("00") + "/" + Año;
        }
    }
    else
        return "";
}
    
asked by Fernando Rumbo 13.02.2018 в 19:21
source

1 answer

1

The problem really occurs in this line:

return "16/" + Mes.ToString("00") + "/" + Año + " - " + Convert.ToDateTime("01/" + (Mes + 1).ToString("00") + "/" + Año).AddDays(-1).ToString("dd") + "/" + Mes.ToString("00") + "/" + Año;

The error is based specifically on the fact that you are doing a conversion to DateTime of not valid, besides that it is not necessary to add one to the month since when it is month 12 it will send you an error, therefore the line of the Following way. Note that to obtain the last day of the month is done with the DateTime.DaysInMonth(Año, Mes) method, sending as parameters the year and the month, with this you no longer need to subtract days or add months:

return "16/" + Mes.ToString("00") + "/" + Año + " - " + new DateTime(Año, Mes, DateTime.DaysInMonth(Año, Mes)).ToString("dd") + "/" + Mes.ToString("00") + "/" + Año;

The complete code would be in the following way:

public static string Quincena_Fecha(string text)
{
    int Año;
    int.TryParse(text.Substring(0, 4), out Año);
    int Mes;
    int Quincena;
    if (int.TryParse(text.Substring(4, 2), out Quincena))
    {
        double Valor = ((double)Quincena / 2);

        Mes = System.Convert.ToInt32((Quincena + 1) / 2);

        if (Valor == System.Convert.ToInt32(Quincena / 2))
        {
            return "16/" + Mes.ToString("00") + "/" + Año + " - " + new DateTime(Año, Mes, DateTime.DaysInMonth(Año, Mes)).ToString("dd") + "/" + Mes.ToString("00") + "/" + Año;
        }
        else
        {
            return "01/" + Mes.ToString("00") + "/" + Año + " - " + "15/" + Mes.ToString("00") + "/" + Año;
        }
    }
    else
        return "";
}

The result would be of the previous execution would be:

  

12/16/2018 - 12/31/2018

    
answered by 13.02.2018 / 19:37
source