+ Reply to Thread
Results 1 to 11 of 11

Calculate Time Duration

  1. #1
    Forum Contributor
    Join Date
    05-17-2012
    Location
    CA, USA
    MS-Off Ver
    Microsoft Excel for Microsoft 365
    Posts
    163

    Calculate Time Duration

    I thought that this would have been a common problem but I haven't found anything similar to my issue.

    For Row 5 starting with Column C and ending with column F, I entered in this data: 8.20 a 11.00 a. Basically I want to calculate duration of time by entering data such that the decimal is equivalent to ":". I.e. 8.20 = 8:20. Maybe there is a better way to solve this issue but I don't know how to subtract time notation if possible, i.e 11:00a - 8:20a = 2.67 hours. What my formula is doing is counting the 2 as a one-digit number rather than in the 10s place (2 instead of 20). Not sure how to amend that to where 8.20 = 500, 8.02 = 482.

    Please Login or Register  to view this content.
    Sorry if my code is a bit inefficient. Data is constantly being added to columns C through columns F.

  2. #2
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Calculate Time Duration

    Try this in cell G5:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Calculate Time Duration

    Alternate formula:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Calculate Time Duration

    Thought of a different method:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    05-17-2012
    Location
    CA, USA
    MS-Off Ver
    Microsoft Excel for Microsoft 365
    Posts
    163

    Re: Calculate Time Duration

    Holy smokes. Thanks for the speedy response. They all work, I'll use your latest code, tigeravatar. Thanks, guys.

  6. #6
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Calculate Time Duration

    Wow, neverthought to use DOLLARDE. *Mind Blown*

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,677

    Re: Calculate Time Duration

    Quote Originally Posted by tigeravatar View Post
    =IF(COUNTA(C5:F5)<4,"",DOLLARDE(E5,60)+IF(AND(E5<12,F5="p"),12,0)-DOLLARDE(C5,60)-IF(AND(C5<12,D5="p"),12,0))
    Perhaps there won't be any times around midnight but what if C5 = 12.3 and D5 = a signifying half past midnight, that formula treats the time like 12:30 PM

    I suggest this version

    =((TEXT(E5*100,"0\:00 ")&F5)-(TEXT(C5*100,"0\:00 ")&D5))*24
    Last edited by daddylonglegs; 05-31-2012 at 02:59 PM.
    Audere est facere

  8. #8
    Forum Contributor
    Join Date
    05-17-2012
    Location
    CA, USA
    MS-Off Ver
    Microsoft Excel for Microsoft 365
    Posts
    163

    Re: Calculate Time Duration

    Quote Originally Posted by daddylonglegs View Post
    Perhaps there won't be any times around midnight but what if C5 = 12.3 and D5 = a signifying half past midnight, that formula treats the time like 12:30 PM

    I suggest this version

    =((TEXT(E5*100,"0\:00 ")&F5)-(TEXT(C5*100,"0\:00 ")&D5))*24
    You do provide a good point about 12:30am for C5 and D5. That just made me realize something else. If C5=11.3 and D5=p while E5=1.3 and F5=a, it provides the hours "owed" instead of the actual hours. Not, that I couldn't solve this but there is probably a more efficient way than formulating it with an if function similar to the one shown below...

    Please Login or Register  to view this content.
    Basically if the value is negative, it adds 24 hours to make it correct or leaves it as is if the difference is already positive. It seems a bit redundant the way I did it...

  9. #9
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,677

    Re: Calculate Time Duration

    Quote Originally Posted by T86157 View Post
    If C5=11.3 and D5=p while E5=1.3 and F5=a, it provides the hours "owed" instead of the actual hours.
    So the answer for that example should be 2? Try this modified version of my suggestion

    =MOD((TEXT(E5*100,"0\:00 ")&F5)-(TEXT(C5*100,"0\:00 ")&D5),1)*24

  10. #10
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Calculate Time Duration

    Ooh, nice use of MOD there. And using TEXT to force excel to treat the times as times for the calculation, very clever I'm impressed as always with your work, dll. Thanks for sharing!

  11. #11
    Forum Contributor
    Join Date
    05-17-2012
    Location
    CA, USA
    MS-Off Ver
    Microsoft Excel for Microsoft 365
    Posts
    163

    Re: Calculate Time Duration

    Yep, answer should be 2, daddylonglegs. You guys are super formula experts. I have never seen half the functions you guys are using in the formulas. Thank you, works like an amulet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1