0

How we can calculate weeks between two dates? I dont see any API. Does any API exsists? How we can do that?

I see there is an API to calculate Months only

Date myDate1 = Date.newInstance(2015, 2, 17);
Date myDate2 = Date.newInstance(2017, 12, 17);
System.debug('Months Between Two :'+myDate1.monthsBetween(myDate2));
System.debug('Weeks Between Two :'+myDate1.weeksBetween(myDate2));

The below line will never works. Any pointers to get weeks difference ?

System.debug('Months Between Two :'+myDate1.weeksBetween(myDate2));
3

there is no method called weeksbetween. You should leverage daysbetween and divide by 7

Date myDate1 = Date.newInstance(2015, 2, 17);
Date myDate2 = Date.newInstance(2017, 12, 17);
System.debug('Months Between Two :'+myDate1.monthsBetween(myDate2));
System.debug('Weeks Between Two :'+myDate1.daysBetween(myDate2)/7);
  • @Satrath - Thanx. This is good workaround. I like it. Apex yet to be matured like Java. – user4567570 Apr 13 '17 at 17:54
1

There is no such method in Date Class, you can do myDate1.daysBetween(myDate2) / 7;

Not the answer you're looking for? Browse other questions tagged or ask your own question.