+ Start a Discussion

get list of days between 2 dates

Hi All


I need to get a list of days between a start and end date, as a test i've tried using the following but it doesn't produce any results (no error message either). Any ideas how i can achieve this?


Date startDate = date.newinstance(2010, 01, 01);
Date endDate = date.newinstance(2011, 01, 01);
        for (Date d = startDate; d.isSameDay(endDate) ; d.addDays(1))

 Thanks for any ideas.


Best Answer chosen by Admin (Salesforce Developers) 
ashish raiashish rai


     Well you have used date.isSameDay method of Date which return type is Boolean,Thats why controller doesn't go inside your for loop condition. You can't iterated like that. But you can use daysBetween method of Date like this:

date startDate =      date.newInstance(2010, 1, 1);

date dueDate =      date.newInstance(2011, 1, 30);

integer numberDaysDue =      startDate.daysBetween(dueDate);

system.debug('@@@@@@@@@@@@' +numberDaysDue );


Think this will help you.