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
nellymnjnellymnj 

datetime function month returns incorrect result

Hello,

I need to get summary data for every month, so in an apex class, I get the initial date as a parameter, it is date datatype, I convert it to datetime (that's what a need to send to the external web service) and iterate through the months. the problem is that i am getting incorrect month number from both datetime.month() and datetime.format('yyyy-MM-dd') functions for 3-rd and 4-th iterations.

Here is sysdem.debug otput for 4 iteratios. First value is the initial datetime dtfrom, second value is the result of datetime dttask = dtfrom.addMonths(i) (i changes from 0 to 3), third value is a result of formatting dttask.format('yyyy-MM-dd'), and 4-th values is dttask.month():

i = 0:

20091211155205.873:Class.Forecasting.GetTasks: line 298, column 4: 2009-10-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 300, column 4: 2009-10-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 302, column 4: 2009-10-01
20091211155205.873:Class.Forecasting.GetTasks: line 308, column 4: 10

i = 1:

 

20091211155205.873:Class.Forecasting.GetTasks: line 298, column 4: 2009-10-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 300, column 4: 2009-11-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 302, column 4: 2009-11-01
20091211155205.873:Class.Forecasting.GetTasks: line 308, column 4: 11

i = 2

 

20091211155205.873:Class.Forecasting.GetTasks: line 298, column 4: 2009-10-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 300, column 4: 2009-12-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 302, column 4: 2009-11-30
20091211155205.873:Class.Forecasting.GetTasks: line 308, column 4: 11
i=3
 20091211155205.873:Class.Forecasting.GetTasks: line 298, column 4: 2009-10-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 300, column 4: 2010-01-01 07:00:00
20091211155205.873:Class.Forecasting.GetTasks: line 302, column 4: 2009-12-31
20091211155205.873:Class.Forecasting.GetTasks: line 308, column 4: 12
so for third iteration formatting returns the previous day and month is the previous month.
what's wrong with that? van anybody please help? Thank you very much for any input. 
Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz
I agree this looks incorrect.  I've filed a bug for it.  Thanks for reporting this.