I'm using the following query to get the next month.
int theMonth = ((System.DateTime)periodStartDate).Month+1;
But if the periodstartDate month id=s december,the above statement throws error.
I think you can get it in this fashion
DateTime dt = new DateTime(2011,12,2);
DateTime dayone = new DateTime(dt.AddMonths(1).Year, dt.AddMonths(1).Month, 1);
Now you have a proper DateTime
object to the first of the next month, do as you please with it
The expression ((System.DateTime)periodStartDate).Month+1
doesn't throw an error if the month is December - it just returns 13. I suspect you're doing this:
var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month + 1, 1);
That would throw an error.
Try this instead:
var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month, 1)
.AddMonths(1);
I like V4V's answer, but I write it this way:
DateTime dt = new DateTime(2011,12,2);
DateTime firstDayNextMonth = dt.AddMonths(1).AddDays(-dt.Day+1);
For example, I might be computing a future time and this code does that without stripping out the time part.
Per hvd's most astute comment, this code should be:
DateTime dt = new DateTime(2011,12,2);
DateTime firstDayNextMonth = dt.AddDays(-dt.Day+1).AddMonths(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With