Skip to main content
BreakawayPaul
Inspiring
May 14, 2014
Question

Adding times together

  • May 14, 2014
  • 1 reply
  • 416 views

I have table with a column that holds elapsed times in an hh:mm format.  I'd like to add these times together to get a total, but I can't add something with a :, and dateadd() isn't a fan either.

Any suggestions?

I thought about breaking apart the hh:mm and converting the hours into straight minutes, then adding them together, but that seems clunky.

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    May 14, 2014

    <cfset time1="01:23">

    <cfset timeInMins1 =60*listgetat(time1,1,":")+listgetat(time1,2,":")>

    <cfset time2="23:45">

    <cfset timeInMins2 =60*listgetat(time2,1,":")+listgetat(time2,2,":")>

    <!--- Add times --->

    <cfset totalMins = timeInMins1+timeInMins2>

    <!--- Pad single digits with 0 --->

    <cfset hrsPart = right('0' & int(totalMins/60), 2)>

    <cfset minsPart = right('0' & totalMins mod 60, 2)>

    hrsPart: <cfoutput>#hrsPart#</cfoutput><br>

    minsPart: <cfoutput>#minsPart#</cfoutput>

    BreakawayPaul
    Inspiring
    May 18, 2014

    Sorry for abandoning the thread.  We have a severe illness in the family that's taken up a lot of our time.

    As for adding the times, that's a bit of yikes-a-roni

    I just found some entries that have hours and some only minutes, so I'd have to contend with that.  Maybe I can could the list items from right to left and stop when I get a null.

    BKBK
    Community Expert
    Community Expert
    May 18, 2014

    The yikes-a-roni is inevitable. To add time values, you must add hours and minutes separately, using modulo 60 arithmetic.