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 "";
}