July 20, 2012

How to find number of days between two dates using Apex?

Integer noOfDays = startDate.daysBetween(endDate);

The above formula is used to find number of days between two dates.
 
Execute the below code in Developer Console to view the output:
 
Date startDate = Date.today();
Date endDate = Date.today().addDays( 90 );
Integer noOfDays = startDate.daysBetween( endDate );
system.debug( 'Output is ' + noOfDays );

 

 

7 comments:

  1. Thank you so much !

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. WHERE WE SHOULD ENTER THIS CODE?

    ReplyDelete
    Replies
    1. Execute the below code in Developer Console to view the output:

      Date startDate = Date.today();
      Date endDate = Date.today().addDays( 90 );
      Integer noOfDays = startDate.daysBetween( endDate );
      system.debug( 'Output is ' + noOfDays );

      Delete