# how to get week between to given dates?

I have two fields Starting date and End date I want to get the week between them with one decimal point that is the extra day if any. suppose 10 days will be soon as 1.3 and 24 as 3.3.

Try this code below.

Date startDate = Date.newInstance(2016, 6, 16);
Date endDate = Date.newInstance(2016, 7, 13);
Integer noOfDays = startDate.daysBetween(endDate);
Integer wholeWeeks = Math.floor(noOfDays / 7).intValue();
Integer daysLeft = noOfDays-(wholeWeeks*7);
System.debug('Days Between = '+noOfDays);
String strWeeksBetwwn = wholeWeeks+'.'+daysLeft;
Decimal decWeeksBetwwn = decimal.valueOf(strWeeksBetwwn);
System.debug('Weeks Between = '+ decWeeksBetwwn);

Output:

USER_DEBUG [6]|DEBUG|Days Between = 27
17:27:09:004 USER_DEBUG [9]|DEBUG|Weeks Between = 3.6

Try this,

Date startDate = Date.newInstance(2016, 8, 16);
Date endDate = Date.newInstance(2016, 8, 26);
Decimal daysBetween = startDate.daysBetween(endDate);

System.debug('Days Between = '+daysBetween);
System.debug('Weeks Between = '+ (daysBetween/7).setScale(1));
• Can you check your answer, for dates 2016, 6, 16 and 2016, 7, 13. It is coming 3.9. Commented Jun 16, 2016 at 12:02