search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Help with future dates

Asked by Michael
12 years ago.

Hi,
I am displaying future months and the code works for the month
Here is the code

checker2.StartDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(1);
checker2.Days = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
Label2.Text = checker2.StartDate.ToString("MMMM yyyy");

The problem is the code for the number of days in the month

Any suggestions as to how I can get it not to reflect this months number of days but the number of days for the month shown in the new month line?

I have tried
checker2.Days = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month + 1)
But this does not work when the new year moves to 2011

Thank you
Michael

Comment posted by Dan Letecky
12 years ago.

You should try AddMonths() method:

DateTime nextMonth = DateTime.Today.AddMonths(1);

You already use it when assigning the StartDate.

The most easy way would be to use StartDate property for the calculation:

checker2.Days = DateTime.DaysInMonth(checker2.StartDate.Year, checker2.StartDate.Month);

Comment posted by Michael
12 years ago.
Thanks Dan it works perfectly now, for anyone else here is the complete code I used

checker2.StartDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(1);
checker2.Days = DateTime.DaysInMonth(checker2.StartDate.Year, checker2.StartDate.Month);
Label2.Text = checker2.StartDate.ToString("MMMM yyyy");
This question is more than 3 months old and has been closed. Please create a new question if you have anything to add.