What I'm trying to achieve
I'm trying to get the number of days of two months (the current) and the next month. Actually I succesfully achieve this, using that code:
int monthDays = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
string[] days = Enumerable.Range(1, monthDays).Select(x => x.ToString("D2")).ToArray();
essentially I used the function DaysInMonth
and then I generated a List<int>
that represents the days of that month.
Problem
Now, I want get also the days of the next month, but I've some problem to handle the following situation:
December 2018 (current)
January 2019 (next)
What I tried
as you can see the year
has changed, so the code that I wrote for get the days of next months will fail:
var nextMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1);
monthDays = DateTime.DaysInMonth(DateTime.Now.Year, nextMonth.Month);
days = Enumerable.Range(1, monthDays).Select(x => x.ToString("D2")).ToArray();
how can I manage the new year in the next
month?