+ Reply to Thread
Results 1 to 9 of 9

Counting time within time periods!

  1. #1
    Registered User
    Join Date
    08-30-2010
    Location
    Cornwall, UK
    MS-Off Ver
    Excel 2007
    Posts
    30

    Counting time within time periods!

    Your all gonna love this one!

    I have a colum A as shift start date / time and colum B of Shift End date / time.

    We have two rates of hourly pay depending on what hours in the day the staff worked. Rate 1 is between 07:00 and 20:00, Rate 2 is between 20:00 and 07:00.

    I'd like to have colum C as hours the staff worked in rate 1, and colum D as number of hours that they worked in rate 2.

    e.g
    Colum A: 06/09/11 03:00 - Start date/time
    Colum B: 06/09/11 23:30 - End date time
    Colum C: 7.5 or 07:30 - hours worked on rate 1 (3am until 7am is 4 hours plus 8pm untill 11:30pm is 3 and a half hours)
    Colum D: 11 or 11:00 (7am until 8pm is 11 hours)

    This sounds really simple, but it has been baking my noodle for a loooooong time. Any ideas?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Counting time within time periods!

    How is 7 AM to 8 PM 11 hours? 8 to 8 is usually 12 hours so 7 to 8 would be 13.


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-30-2010
    Location
    Cornwall, UK
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Counting time within time periods!

    Quote Originally Posted by TMShucks View Post
    How is 7 AM to 8 PM 11 hours? 8 to 8 is usually 12 hours so 7 to 8 would be 13.


    Regards
    Typo


    Regards

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Counting time within time periods!

    Assuming data in row 2:

    C2: =(B2-A2)-((MIN((B2-INT(B2)),TIME(20,0,0)))-(MAX((A2-INT(A2)),TIME(7,0,0))))

    D2: =(MIN((B2-INT(B2)),TIME(20,0,0)))-(MAX((A2-INT(A2)),TIME(7,0,0)))


    Note: some extra brackets to separate the stages ;-)


    Regards

  5. #5
    Registered User
    Join Date
    08-30-2010
    Location
    Cornwall, UK
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Counting time within time periods!

    Quote Originally Posted by TMShucks View Post
    Assuming data in row 2:

    C2: =(B2-A2)-((MIN((B2-INT(B2)),TIME(20,0,0)))-(MAX((A2-INT(A2)),TIME(7,0,0))))

    D2: =(MIN((B2-INT(B2)),TIME(20,0,0)))-(MAX((A2-INT(A2)),TIME(7,0,0)))


    Note: some extra brackets to separate the stages ;-)


    Regards
    Ideal... works great... almost.

    06/09/11 04:00 - 06/09/11 04:30 - returns a value of 03:00 instead of 00:30

    Any ideas?

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Counting time within time periods!

    Maybe:

    C2: =IF((A2-INT(A2))<TIME(7,0,0), MIN((B2-INT(B2)),TIME(7,0,0))-(A2-INT(A2)),0)+IF((B2-INT(B2))>TIME(20,0,0), (B2-INT(B2))-MAX((A2-INT(A2)),TIME(20,0,0)),0)

    D2: =(B2-A2)-(IF((A2-INT(A2))<TIME(7,0,0), MIN((B2-INT(B2)),TIME(7,0,0))-(A2-INT(A2)),0)+IF((B2-INT(B2))>TIME(20,0,0), (B2-INT(B2))-MAX((A2-INT(A2)),TIME(20,0,0)),0))


    Regards

  7. #7
    Registered User
    Join Date
    08-30-2010
    Location
    Cornwall, UK
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Counting time within time periods!

    Quote Originally Posted by TMShucks View Post
    Maybe:

    C2: =IF((A2-INT(A2))<TIME(7,0,0), MIN((B2-INT(B2)),TIME(7,0,0))-(A2-INT(A2)),0)+IF((B2-INT(B2))>TIME(20,0,0), (B2-INT(B2))-MAX((A2-INT(A2)),TIME(20,0,0)),0)

    D2: =(B2-A2)-(IF((A2-INT(A2))<TIME(7,0,0), MIN((B2-INT(B2)),TIME(7,0,0))-(A2-INT(A2)),0)+IF((B2-INT(B2))>TIME(20,0,0), (B2-INT(B2))-MAX((A2-INT(A2)),TIME(20,0,0)),0))


    Regards
    Very close mate, and I do appreciate all your help.

    however....

    26/08/2011 05:45 27/08/2011 02:00 produces a negative
    and
    05/09/2011 17:00 06/09/2011 01:30 produces a zero.

    do you know why this might be?

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Counting time within time periods!

    do you know why this might be?
    Because you keep moving the goalposts, maybe? ;-)

    Your previous examples were for the same day.

    I'll need to sleep on it.

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

    Re: Counting time within time periods!

    What's the maximum shift length? Assuming that the end date will always be either the same day or the next (compared to start date) then try this formula in C2

    =(INT(B2)>INT(A2))*13+MEDIAN(MOD(B2,1)*24,7,20)-MEDIAN(MOD(A2,1)*24,7,20)

    and in D2

    =(B2-A2)*24-C2

    format C2 and D2 as number with 2 decimal places

    [note the second formula uses the result of the first]

    These will give the results as a decimal number of hours, e.g. for your last two examples you'll get 13 and 7.25 for the first and 3 and 5 for the second - is that correct?
    Audere est facere

+ 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