how to find difference of minutes between 2 time parameter

Certified Senior Developer

how to find difference of minutes between 2 time parameter.

For example i have StartTime: 6:00 PM, and EndTime: 7:30PM.

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    You can subtract end time -  start time

    time(19, 30) - time(18, 00) = 1:30 AM


  • If you need to find the exact time difference in minutes, then this code might help you:

    (hour(ri!time2 - ri!time1) * 60) + minute(ri!time2 - ri!time1)

  • 0
    Certified Lead Developer

    This is a good use case for a strong helper rule that can handle various similar use cases.  Years ago I created a rule that will calculate the elapsed seconds between any two DateTime rule inputs.  You can then calculate the minutes from this by dividing the result by 60 and rounding to whatever level of specificity you require.

    /* rule!GLBL_CalculateElapsedSeconds */
    /* ri!startTime and ri!endTime are both DateTime type */
    
    a!localVariables(
      local!days: tointeger(ri!endTime - ri!startTime),
      local!hours: hour(ri!endTime - ri!startTime),
      local!minutes: minute(ri!endTime - ri!startTime),
      local!seconds: second(ri!endTime - ri!startTime),
    
      if(
        local!days = 0,
        0,
        local!days * 24 * 60 * 60
      )
    
      +
    
      if(
        local!hours = 0,
        0,
        local!hours * 60 * 60
      )
    
      +
    
      if(
        local!minutes = 0,
        0,
        local!minutes * 60
      )
    
      +
    
      local!seconds
    )

    ( Note: no need to @ me that the above code could be condensed / tightened up quite a bit... i wrote it a few years ago and have left it overly verbose for the purposes of ease of understanding - at least that's my official excuse ;-) )

  • There are a few ways to accomplish this one, another option is to subtract the datetime values creating an interval and parse out the result to obtain minutes as:

    a!localVariables(
      local!date1: datetime(2021,9,7,6,30),
      local!date2: datetime(2021,9,7,7,05),
      local!interval: local!date2-local!date1,
      local!days: split(local!interval,"::")[1],
      local!hours: split(split(local!interval,"::")[2],":")[1],
      local!minutes: split(split(local!interval,"::")[2],":")[2],
      
      local!days*24*60
      +
      local!hours*60
      +
      local!minutes  
    )

  • 0
    Certified Lead Developer

    I usually use the below expressions:

    Expression to find the difference between time parameter

    a!localVariables(
      local!time1: time(5, 00),
      local!time2: time(10, 35),
      local!differenceInMins: todecimal(local!time2 - local!time1) * 24 * 60,
      local!differenceInMins
    )

    Expression to find the difference between date-time parameter

    a!localVariables(
      local!datetime1: datetime(2021,08,09,12,00,00),
      local!datetime2: datetime(2021,08,10,14,00,00),
      local!differenceInMins: todecimal(local!datetime2 - local!datetime1) * 24 * 60,
      local!differenceInMins
    )