[Solved] How to get the last day of each month?


If I’m understanding this correctly, what you’re essentially doing is taking the month, adding 1 to it so it is actually the next month. Then you subtract a day from it so it’s the last day of the previous month. It would just be better to give it the actual date instead of doing a round-about sort of way.

Replace

DateTime fechaFinal = new DateTime(Convert.ToInt32(ddlAñoFinal.SelectedValue), 
                                   Convert.ToInt32(ddlMesFinal.SelectedValue) +1, 
                                   01).AddDays(-1);

with

int añoFinal = Convert.ToInt32(ddlAñoFinal.SelectedValue);
int mesFinal = Convert.ToInt32(ddlMesFinal.SelectedValue);
int díaFinal = DateTime.DaysInMonth(añoFinal, mesFinal);
DateTime fechaFinal = new DateTime(añoFinal, mesFinal, díaFinal);

3

solved How to get the last day of each month?