Loading

Calculating Business Hours Between Two Timestamps

Publiseringsdato: Feb 24, 2026
Oppgave
How to calculate the number of business hours between two timestamps.

For example to calculate the time spent on a project based on two Date & Time fields [Start] and [End].
Trinn

These directions are demonstrated in the example workbook downloadable from the right-hand pane of this article. In this example, the work week is Monday to Friday, and the business hours are 8:15am to 5:15pm. Adjust calculations as needed for the desired business hours.

Step 1: Shift start and end to business hours

To exclude non-business minutes before opening and after closing, we can shift the the start date to the next opening time and the end date to the previous closing time.

  1. Select Analysis > Create Calculated Field
  2. In the Calculated Field dialog box that opens, do the following, and then click OK:
    1. Name the calculated field. In this example, the calculated field is named "Start (shift to opening)"
    2. In the formula field, create a calculation similar to the following:
      IF DATEPART('weekday', [Start]) = 7 //if the start date is a Saturday
      THEN 
          DATEADD('minute', 8*60 + 15, DATETRUNC('day', 
          //these expressions shift to opening time of 8:15am aka 8:15 on the 24hr clock
          //adjust the interval to the desired open of business hours
             DATEADD('day', 2, [Start])
             //add two days to move to Monday
          ))
      
      ELSEIF DATEPART('weekday', [Start]) = 1 //if the start date is a Sunday
      THEN 
          DATEADD('minute', 8*60 + 15, DATETRUNC('day', DATEADD('day', 1, [Start])))
      
      ELSE 
          MAX(
              [Start],
              DATEADD('minute', 8*60 + 15, DATETRUNC('day', [Start]))
          )
          //the MAX() aggregation will return whichever timestamp is last:  
          //the actual start time or the business opening time on the same day
      END
      
  3. Create a calculated field with a name like "End (shift to closing)" with a calculation similar to the following:
    IF DATEPART('weekday', [End]) = 7
    THEN 
        DATEADD('minute', 17*60 + 15, DATETRUNC('day', 
        //these expressions shift to closing time of 5:15pm, aka 17:15 on the 24hr clock
           DATEADD('day', -1, [End])
        ))
    
    ELSEIF DATEPART('weekday', [End]) = 1
    THEN DATEADD('minute', 17*60 + 15, DATETRUNC('day', DATEADD('day', -2, [End])))
    
    ELSE MIN([End], DATEADD('minute', 17*60 + 15, DATETRUNC('day', [End])))
    END
    


Step 2: Find business hours

Next we need to remove non-business hours (i.e. the time after closing each day to opening the next day), as well as "business hours" on weekend days.

  1. Create a calculated field with a name like "Business Minutes" with a calculation similar to the following:
    DATEDIFF('minute', [Start (shifted to opening)],[End (shifted to closing)])
    //total minutes between start and end times
    
    - DATEDIFF('minute', #4/3/2023 5:15 pm#, #4/4/2023 8:15 am#)
    //find non-business minutes per day by finding the difference between the closing time 
    //on one arbitrary date and opening time on the next day
    * DATEDIFF('day', [Start (shifted to opening)],[End (shifted to closing)])
    //multiply by the number of days between Start and End to remove non-business minutes each day
    
    - DATEDIFF('minute', #4/3/2023 8:15 am#,#4/3/2023 5:15 pm#) 
    * 2 
    * DATEDIFF('week', [Start (shifted to opening)], [End (shifted to closing)])
    //for every week subtract the 'business hours' for 2 weekend days
    
  2. Create a calculated field with a name like "Business Hours" with a calculation similar to the following:
    [Business Minutes]/60
    
Flere ressurser
  • These calculated fields can be combined. For these instructions the calculated fields are separated to make it easier to follow what the calculations are doing.
  • The final [Business Hours] calculation cannot be used in a Gantt bar visualization because a Gantt bar cannot skip over time. See the "Gantt" worksheet in the attached workbook for an example of a Gantt bar.

 
Knowledge-artikkelnummer

001456485

Vedlegg

Business Hours_v2022.1.twbx

49 KB

 
Laster
Salesforce Help | Article