function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
raorao 

Getting weird result using Date.AddMonths() method

Date endDate = date.parse('4/30/2017');
Date startDate = endDate.addMonths(-1);

Start date is now coming as 3/30/2017, where as it should be 3/31/2017.  Any suggestions.
Hemant_SoniHemant_Soni
Hi rao,

I Think you need use 
startDate = endDate.addDays(-30);
To get your result.
I think this may help you.

Thanks
Hemant
 
Raj VakatiRaj Vakati
Use this approach  

 
Date endDate = date.parse('2/28/2017');
Integer days = Date.daysInMonth(System.today().Year(), System.today().month()) ;
System.debug('days'+days);
Date startDate = endDate.addDays(-days);
SYstem.debug('startDate'+startDate);
Manohar kumarManohar kumar

Hi rao,

I think addMonths is working correctly, its changing your month from 4 to 3. May be you want to change days instead of month.

Hope this will help you.

Thanks,

Manohar