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

Parents
  • 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 ;-) )

Reply
  • 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 ;-) )

Children
No Data