Skip to main content
Hi,

I have the following code:

public static string getBusinessHours(ID CaseId){

        string TimeDiff;

        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

        Case c = [Select id,CreatedDate from case where id=:CaseId];

        

        Datetime CurrentDateTime = System.now();

        Datetime CreatedDateTime = c.CreatedDate ;

        //Datetime ClosedDateTime = ncs[i].ClosedDate ; 

        //Datetime ClosedDateTime =ncs[i].LastModifiedDate;

    

        Decimal diffHours = BusinessHours.diff(bh.ID,CreatedDateTime,CurrentDateTime);

Decimal days= (diffHours/86400000).round();  // converting milliseconds into days

Decimal hours = (diffHours/3600000).round();  // converting milliseconds into Hours

        Decimal minutes = (diffHours/60000).round();  // converting milliseconds into minutes

        Decimal seconds = (diffHours/1000).round();  // converting milliseconds into seconds

        

        TimeDiff = hours + ':' + minutes + ':' + seconds;

             

        return TimeDiff;

    }

This will return the days:hours:mins:seconds. But if the created date is 25/9/2018, the result is 80:1922:115331:6919867

But I want it to be displayed as 80 days and number of minutes after those many hours and seconds after those many minutes.

I don't want the total number of hours, minutes and seconds from the created date.

Can anyone please help on this?

Regards,

Anketha
3 answers
  1. Jul 3, 2019, 6:27 PM
    Hi Anketha,

    Try the following code it may be helpful for you:

    public static string getBusinessHours(ID CaseId){

            string TimeDiff;

            BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

            Case c = [Select id,CreatedDate from case where id=:CaseId];

            

            Datetime CurrentDateTime = System.now();

            Datetime CreatedDateTime = c.CreatedDate ;

            //Datetime ClosedDateTime = ncs[i].ClosedDate ; 

            //Datetime ClosedDateTime =ncs[i].LastModifiedDate;

        

            Decimal diffHours = BusinessHours.diff(bh.ID,CreatedDateTime,CurrentDateTime);

            Decimal days= (diffHours/86400000).round();  // converting milliseconds into days

            Decimal hours = (diffHours/3600000).round();  // converting milliseconds into Hours

            Decimal minutes = (diffHours/60000).round();  // converting milliseconds into minutes

            Decimal seconds = (diffHours/1000).round();  // converting milliseconds into seconds

            

             Decimal newHours=days*24-hours;

                Decimal newminutes=((24*days*60)+newHours*60)-minutes;

                Decimal newseconds=((24*days*60*60)+(newHours*60+newminutes*60))-seconds;

            TimeDiff = days +':'+ newHours + ':' + newminutes + ':' + newseconds;

                 

            return TimeDiff;

        }

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Deepali Kulshrestha.
0/9000