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
myat myatmyat myat 

Date.monthsBetween

Date.monthsBetween method returns one day less. Is it how date method working?
eg.
date fDate = date.newInstance(2018, 09, 01);
date sDate = date.newInstance(2018, 09, 30);
system.debug(fDate.monthsBetween(sDate));

Thanks.
Myat
Raj VakatiRaj Vakati
It will run based on the time zone of the Org .. please check what is your salesforce compnay orgniation time zone and see the result .. 

GO to setup --> find company infomrmation ..
devedeve
Hi myat,

This link can help you https://success.salesforce.com/answers?id=90630000000CyvHAAS
Ajay K DubediAjay K Dubedi
Hi myat,

The result of your code will return you a Zero value as both the dates are of the same month.
If you replace the Date.monthsBetween with Date.daysBetween then you will notice that the 
code is 100% correct and you can go with it.

Have a look at this code:

public class Dateclass {
  public static void checkdate()
  {
    date fDate = date.newInstance(2018, 09, 01);
    date sDate = date.newInstance(2018, 09, 04);
    system.debug(fDate.daysBetween(sDate));

  }
}
OUTPUT:
29

I hope you this explanation will help you. Please mark it as best answer if you find it helpful.

Thanks.
Ajay Dubedi
myat myatmyat myat
Based on our business requirement, it should be be 1 months. Seems it's how date method working and I added to the End Date to solve the issue.
Thanks guys for the suggestion.

Regards,
myat