If I have var olddate = DateTime.Parse('05/13/2012');
and I want to get var newdate = (the first of the month after olddate, 06/01/2012 in this case);
What would I code? I tried to set the month+1 but month has no setter.
Try this simple one-liner:
var olddate = DateTime.Parse("05/13/2012");
var newdate = olddate.AddDays(-olddate.Day + 1).AddMonths(1);
// newdate == DateTime.Parse("06/01/2012")
Try this:
olddate = olddate.AddMonths(1);
DateTime newDate = new DateTime(olddate.Year, olddate.Month, 1,
0, 0, 0, olddate.Kind);
This won't ever cause out-of-range errors, and will preserve the DateTime
's Kind.
dt = dt.AddMonths(1.0);
dt = new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind);
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