I am trying to calculate the weekend days between two entered dates using the following code:
public int CalcularDias(DateTime fecha, DateTime fin)
{
int contador = 0;
DateTime dtInicio = fecha;
DateTime dtFin = fin;
for (int k = dtInicio.Day; k < dtFin.Day;k++)
{
while (dtInicio.DayOfWeek == DayOfWeek.Saturday || dtInicio.DayOfWeek == DayOfWeek.Sunday)
{
dtInicio = dtInicio.AddDays(1);
contador++;
}
dtInicio = dtInicio.AddDays(1);
}
return contador;
}
The problem I have is that when the day of the date is greater than the date to compare it gives me a calculation error and I am trying to solve this error