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
pq100pq100 

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))
        {
            system.debug(d);
        }

 Thanks for any ideas.

Paul

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

Hello,

     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.