Add months and set the last day of the month in one step
-
I want to add months and set the day as last day of that added month. I know i can do in the following way,
DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months));
The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it? -
I want to add months and set the day as last day of that added month. I know i can do in the following way,
DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months));
The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it?Hi, from year and month:
DateTime dt=new DateTime(year, month, 1).AddMonths(monthsToAdd+1).AddDays(-1);
or from DateTime:
dt=dt.AddMonths(monthToAdd+1).AddDays(-dt.Day); // incorrect at end of month
dt=dt.AddDays(1-dt.Day).AddMonths(monthToAdd+1).AddDays(-1); // fixed:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
modified on Wednesday, February 3, 2010 10:08 PM
-
I want to add months and set the day as last day of that added month. I know i can do in the following way,
DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months));
The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it?fixed a bug :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Hi, from year and month:
DateTime dt=new DateTime(year, month, 1).AddMonths(monthsToAdd+1).AddDays(-1);
or from DateTime:
dt=dt.AddMonths(monthToAdd+1).AddDays(-dt.Day); // incorrect at end of month
dt=dt.AddDays(1-dt.Day).AddMonths(monthToAdd+1).AddDays(-1); // fixed:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
modified on Wednesday, February 3, 2010 10:08 PM
Thanks Luc!! Why didnt it strike in my mind... :)
-
Thanks Luc!! Why didnt it strike in my mind... :)
you're welcome. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]