Skip to main content
April 20, 2023
Solved

Calculate Business hours & Minutes between two date & Time

  • April 20, 2023
  • 3 replies
  • 0 views

Hi Team,

Looking for a function which can provide hours and minute difference between two date and time considering business calendar.

calworkhours() only provide hours plus it rounds off it to nearest hour.

Looking for below calculation :

Start Time : 20/04/2023 10:00 AM

End Time : 24/04/2023 11:30 AM

Output -  hours : 17 , minutes 30

Calworkhours output - hours : 18

    Best answer by sanchitg0002

    OOTB calendar functions seems to be missing a function that calculate work minutes between two datetimes.

    You can use the free Calendar function plugin available in Appmarket and use the below expression.

    a!localVariables(
      local!minutes: calworkminutesplugin(ri!startTime, ri!endTime),
      a!map(
        hours: quotient(local!minutes, 60),
        minutes: mod(local!minutes, 60)
      )
    )

    3 replies

    April 20, 2023

    OOTB calendar functions seems to be missing a function that calculate work minutes between two datetimes.

    You can use the free Calendar function plugin available in Appmarket and use the below expression.

    a!localVariables(
      local!minutes: calworkminutesplugin(ri!startTime, ri!endTime),
      a!map(
        hours: quotient(local!minutes, 60),
        minutes: mod(local!minutes, 60)
      )
    )

    April 20, 2023

    Hi please refer to the below code, without plugin

    a!localVariables(
      local!startdate: datetime(2023, 4, 20, 10, 0, 0),
      local!enddate: datetime(2023, 4, 24, 11, 30, 0),
      local!workday: calworkdays(local!startdate, local!enddate),
      local!HoursofStartdate: hour(local!startdate),
      local!Hoursofenddate: hour(local!enddate),
      local!MinutesofStartdate: minute(local!startdate),
      local!Minutesofenddate: minute(local!enddate),
      local!UpdateHoursofStartdate: if(
        local!HoursofStartdate >= 9,
        local!HoursofStartdate - 9,
        5 - local!HoursofStartdate
      ),
      local!daycount: if(
        local!MinutesofStartdate = 0,
        local!UpdateHoursofStartdate,
        if(
          local!HoursofStartdate >= 9,
          local!UpdateHoursofStartdate + 1,
          local!UpdateHoursofStartdate - 1
        ),
        
      ),
      local!firstdaycount: if(
        local!HoursofStartdate >= 9,
        8 - local!daycount,
        local!daycount
      ),
      local!Mcount: if(
        local!MinutesofStartdate = 0,
        0,
        60 - local!MinutesofStartdate
      ),
      local!lastdayCountcount2: local!Hoursofenddate - 9,
      local!x: local!Minutesofenddate + local!Mcount,
      local!y: if(local!x > 59, mod(local!x, 60), local!x),
      local!total1: if(
        local!x > 59,
        local!lastdayCountcount2 + local!firstdaycount + 1,
        local!lastdayCountcount2 + local!firstdaycount
      ),
      local!total: a!forEach(
        items: enumerate(local!workday - 2) + 1,
        expression: local!total1 + 8 * fv!index
      ),
      local!res: local!total[local!workday - 2] & "Hour " & local!y & "minute",
      local!res
    )

    April 20, 2023

    Salute to the effort you have put in.

    I don't even have courage to understand this.