How to find the difference between two dates

  • Hi friends i'm new to development, and I have a requirement to write a sample query to fetch data in cases for a particular record type.

    I fetch accountIds from cases. Then, I fetch opportunity id's from from account.

    But my problem is there is a field called "Date_Order_Received__c" in cases.So i have to filter the records based on date field in case and any opportunity closed won with "Closedate" with in 180 days(Between both fields).

    My sample code

    List<Case> c=new List<Case>();
    public date Churndate;
    //List<Date> Churndate=new List<Date>();
    c=[Select Id,Date_Order_Received__c,accountId from case where RecordTypeId =  
    System.debug('Data in Case '+c);
    List<Id> accountIds= new List<Id>();
    List<Account> accounts=new List<Account>();
    List<Id> opps=new List<Id>();
    for(Case cl:c) 
    System.debug('Date value in '+churndate);
    for(Account ao:[Select Id,name,(select Id,name from opportunities) from account where Id in:accountIds]) {
        for(Opportunity opp : ao.opportunities)
  • You can use Apex provided Date class method daysBetween:


    Date startDate = Date.newInstance(2008, 1, 1);
    Date dueDate = Date.newInstance(2008, 1, 30);
    Integer numberDaysDue = startDate.daysBetween(dueDate);

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used