Determining peak/off-peak durations between time stamps with varied peak hour ranges

ChevOKeefe

New Member
Joined
Apr 25, 2018
Messages
4
Hello,

I am reporting on events that can occur at any point and for any given duration. I am given a timestamp for the start of the event, and a timestamp for the end of the event, so can work out the duration easy enough. I need to, however, determine how many hours of this event occurred outwith normal operating hours "Off-peak range", with normal operating hours changing depending on the day. The event can also last several days (60+ hours)

E1 - Time stamp Start Date - DD/MM/YYYY hh:mm:ss
F1 - Time Stamp End Date - DD/MM/YYYY hh:mm:ss
Duration - F1-E1 ((h) mm:ss)

I can define the day (=TEXT(value,"DDD") of each time stamp, and also split time from date using "text to columns", but I'm struggling for a quicker or less manual way than going through every line to determine how many hours of the event occured in the "normal range" and how many in the "off-peak" range.

Normal Hours
Mon-Thur - 07:00 to 21:00
Fri - 07:00 to 19:00
Sat,Sun - 08:00-16:00

an example is below;

EventTime Stamp StartTime Stamp EndDurationStart DayHours outside Normal Operation (Off-peak)
1
02/03/2018 01:28

<tbody>
</tbody>
04/03/2018 13:49

<tbody>
</tbody>
60:20:38

<tbody>
</tbody>
Fri
2
12/03/2018 11:37

<tbody>
</tbody>
12/03/2018 20:19

<tbody>
</tbody>
8:41:21

<tbody>
</tbody>
Mon

<tbody>
</tbody>
3
18/01/2018 03:19

<tbody>
</tbody>
18/01/2018 03:48

<tbody>
</tbody>
0:29:43

<tbody>
</tbody>
Thu
4
19/01/2018 13:49

<tbody>
</tbody>
19/01/2018 16:04

<tbody>
</tbody>
2:15:16

<tbody>
</tbody>
Fri
5
20/01/2018 13:35

<tbody>
</tbody>

<tbody>
</tbody>
21/01/2018 11:00

<tbody>
</tbody>

<tbody>
</tbody>
21:24:26

<tbody>
</tbody>

<tbody>
</tbody>
Sat

<tbody>
</tbody>
6
08/12/2017 13:39

<tbody>
</tbody>
10/12/2017 11:02

<tbody>
</tbody>
45:22:59

<tbody>
</tbody>
Fri

<tbody>
</tbody>

<tbody>
</tbody>


Thanks in advance for any help with this.

Chev
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, Chev

suggest a code solution. to implement, ALT-F11 from Excel worksheet and in the VBA insert a code module with function code below (please google for extra info on this process if required, or ask again)

within Excel, enter formula like =OffPeak(cell with start time, cell with end time)
format same as duration cells

code doesn't include error handling - which means that so long as the inputs are good, all should be OK. But if inputs are wrong in some way, anything could happen :)

Code:
Function OffPeak(ByVal StartTime As Date, ByVal EndTime As Date) As Date


    'https://www.mrexcel.com/forum/excel-questions/1053216-determining-peak-off-peak-durations-between-time-stamps-varied-peak-hour-ranges.html
    
    'Calculate hours between StartTime & EndTime outside normal hours
    
    'Normal Hours
    'Mon-Thur - 07:00 to 21:00
    'Fri - 07:00 to 19:00
    'Sat,Sun - 08:00-16:00


    Dim StartDay As Date
    Dim EndDay As Date
    Dim loopDay As Date
    
    Dim PeakStartTimeForLoopDay As Date
    Dim PeakEndTimeForLoopDay As Date
    
    Dim EventStartTimeForLoopDay As Date
    Dim EventEndTimeForLoopDay As Date
    
    
    OffPeak = 0
    StartDay = Int(StartTime)
    EndDay = Int(EndTime)
    
    'Loop through each day, accumulating off-peak hours day by day
    For loopDay = StartDay To EndDay
                
        'Daily limits for this loop day
        Select Case Format$(loopDay, "ddd")
            Case "Mon", "Tue", "Wed", "Thu"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 21 / 24
            Case "Fri"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 19 / 24
            Case "Sat", "Sun"
                PeakStartTimeForLoopDay = 8 / 24
                PeakEndTimeForLoopDay = 16 / 24
        End Select
        
        'if the event starts on this loop day
        If StartDay = loopDay Then
            EventStartTimeForLoopDay = StartTime - Int(StartTime)
        Else
            'the event started at an earlier day, so begins at midnight (zero hours) on this loop day
            EventStartTimeForLoopDay = 0
        End If
        
        'if the event ends on this loop day
        If EndDay = loopDay Then
            EventEndTimeForLoopDay = EndTime - Int(EndTime)
        Else
            'the event ends on a later day, so ends at midnight (24 hours) on this loop day
            EventEndTimeForLoopDay = 1
        End If


        'Accumulate hours on this loop day before this day's peak time
        If EventStartTimeForLoopDay < PeakStartTimeForLoopDay Then OffPeak = OffPeak + PeakStartTimeForLoopDay - EventStartTimeForLoopDay
        'Accumulate hours on this loop day after this day's peak time
        If EventEndTimeForLoopDay > PeakEndTimeForLoopDay Then OffPeak = OffPeak + EventEndTimeForLoopDay - PeakEndTimeForLoopDay
        
    Next loopDay
    
End Function
 
Upvote 0
Hi, Chev

suggest a code solution. to implement, ALT-F11 from Excel worksheet and in the VBA insert a code module with function code below (please google for extra info on this process if required, or ask again)

within Excel, enter formula like =OffPeak(cell with start time, cell with end time)
format same as duration cells

code doesn't include error handling - which means that so long as the inputs are good, all should be OK. But if inputs are wrong in some way, anything could happen :)

Code:
Function OffPeak(ByVal StartTime As Date, ByVal EndTime As Date) As Date


    'https://www.mrexcel.com/forum/excel-questions/1053216-determining-peak-off-peak-durations-between-time-stamps-varied-peak-hour-ranges.html
    
    'Calculate hours between StartTime & EndTime outside normal hours
    
    'Normal Hours
    'Mon-Thur - 07:00 to 21:00
    'Fri - 07:00 to 19:00
    'Sat,Sun - 08:00-16:00


    Dim StartDay As Date
    Dim EndDay As Date
    Dim loopDay As Date
    
    Dim PeakStartTimeForLoopDay As Date
    Dim PeakEndTimeForLoopDay As Date
    
    Dim EventStartTimeForLoopDay As Date
    Dim EventEndTimeForLoopDay As Date
    
    
    OffPeak = 0
    StartDay = Int(StartTime)
    EndDay = Int(EndTime)
    
    'Loop through each day, accumulating off-peak hours day by day
    For loopDay = StartDay To EndDay
                
        'Daily limits for this loop day
        Select Case Format$(loopDay, "ddd")
            Case "Mon", "Tue", "Wed", "Thu"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 21 / 24
            Case "Fri"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 19 / 24
            Case "Sat", "Sun"
                PeakStartTimeForLoopDay = 8 / 24
                PeakEndTimeForLoopDay = 16 / 24
        End Select
        
        'if the event starts on this loop day
        If StartDay = loopDay Then
            EventStartTimeForLoopDay = StartTime - Int(StartTime)
        Else
            'the event started at an earlier day, so begins at midnight (zero hours) on this loop day
            EventStartTimeForLoopDay = 0
        End If
        
        'if the event ends on this loop day
        If EndDay = loopDay Then
            EventEndTimeForLoopDay = EndTime - Int(EndTime)
        Else
            'the event ends on a later day, so ends at midnight (24 hours) on this loop day
            EventEndTimeForLoopDay = 1
        End If


        'Accumulate hours on this loop day before this day's peak time
        If EventStartTimeForLoopDay < PeakStartTimeForLoopDay Then OffPeak = OffPeak + PeakStartTimeForLoopDay - EventStartTimeForLoopDay
        'Accumulate hours on this loop day after this day's peak time
        If EventEndTimeForLoopDay > PeakEndTimeForLoopDay Then OffPeak = OffPeak + EventEndTimeForLoopDay - PeakEndTimeForLoopDay
        
    Next loopDay
    
End Function

hi Fazza, thanks for this and the quick response, you have no idea how much it's appreciated. Though it appeared to work, sense checking has returned an issue. Below is the data using the code and Offpeak function as per your excellent instruction. The issue appears to be occurring on short duration stops (see bottom 2 rows and row 4 below), where the "off peak" hours is being recorded as a higher value than the stop duration itself. It's almost there!


13/12/2017 11:0013/12/2017 13:452:45:01Fri0:00:00
29/12/2017 02:0329/12/2017 10:588:54:54Tue4:56:35
09/01/2018 09:1709/01/2018 09:290:12:21Thu0:00:00
18/01/2018 03:1818/01/2018 03:470:29:02Wed3:41:37
31/01/2018 10:2331/01/2018 10:230:00:32Wed0:00:00
31/01/2018 10:2531/01/2018 14:354:10:20Mon0:00:00
26/02/2018 11:5526/02/2018 15:453:50:20Mon0:00:00
26/02/2018 15:4826/02/2018 15:570:08:58Fri0:00:00
02/03/2018 05:0902/03/2018 11:446:34:47Mon1:50:25

<tbody>
</tbody>
02/03/2018 01:2804/03/2018 13:4960:20:38Mon34:31:12

<tbody>
</tbody>
18/12/2017 23:1618/12/2017 23:210:05:01Mon2:21:06
20/12/2017 01:0120/12/2017 01:060:05:00Wed5:58:21

<tbody>
</tbody>
18/03/2018 21:0318/03/2018 21:290:25:14Sun5:29:12

<tbody>
</tbody>
 
Upvote 0
Error in my thinking. This is better,
Code:
Function OffPeak(ByVal StartTime As Date, ByVal EndTime As Date) As Date


'==============================================
'   'https://www.mrexcel.com/forum/excel-questions/1053216-determining-peak-off-peak-durations-between-time-stamps-varied-peak-hour-ranges.html
'
'   Calculate hours between StartTime & EndTime outside normal hours.
'   Normal hours are hard-coded into Select Case code,
'       Mon-Thur - 07:00 to 21:00
'       Fri - 07:00 to 19:00
'       Sat,Sun - 08:00-16:00
'
'
'   Date    Change
'   ------------------------------------------------------------------------------
'   26-Apr-18 Created
'   27-Apr-18 Fix up handling of finishing before the peak starts, or starting after it ends.
'
'==============================================
    
    Dim StartDay As Date
    Dim EndDay As Date
    Dim loopDay As Date
    
    Dim PeakStartTimeForLoopDay As Date
    Dim PeakEndTimeForLoopDay As Date
    
    Dim EventStartTimeForLoopDay As Date
    Dim EventEndTimeForLoopDay As Date
    
    
    OffPeak = 0
    StartDay = Int(StartTime)
    EndDay = Int(EndTime)
    
    'Loop through each day, accumulating off-peak hours day by day
    For loopDay = StartDay To EndDay
                
        'Daily limits for this loop day
        Select Case Format$(loopDay, "ddd")
            Case "Mon", "Tue", "Wed", "Thu"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 21 / 24
            Case "Fri"
                PeakStartTimeForLoopDay = 7 / 24
                PeakEndTimeForLoopDay = 19 / 24
            Case "Sat", "Sun"
                PeakStartTimeForLoopDay = 8 / 24
                PeakEndTimeForLoopDay = 16 / 24
        End Select
        
        'if the event starts on this loop day
        If StartDay = loopDay Then
            EventStartTimeForLoopDay = StartTime - Int(StartTime)
        Else
            'the event started at an earlier day, so begins at midnight (zero hours) on this loop day
            EventStartTimeForLoopDay = 0
        End If
        
        'if the event ends on this loop day
        If EndDay = loopDay Then
            EventEndTimeForLoopDay = EndTime - Int(EndTime)
        Else
            'the event ends on a later day, so ends at midnight (24 hours) on this loop day
            EventEndTimeForLoopDay = 1
        End If


        'Accumulate hours on this loop day before this day's peak time
        If EventStartTimeForLoopDay < PeakStartTimeForLoopDay Then
            'There are two possibilities,
            '1. the event finishes before the peak starts,
            If EventEndTimeForLoopDay < PeakStartTimeForLoopDay Then
                OffPeak = OffPeak + EventEndTimeForLoopDay - EventStartTimeForLoopDay
            Else
            '2. the event is ongoing when the peak starts
                OffPeak = OffPeak + PeakStartTimeForLoopDay - EventStartTimeForLoopDay
            End If
        End If
        
        'Accumulate hours on this loop day after this day's peak time
        If EventEndTimeForLoopDay > PeakEndTimeForLoopDay Then
            'There are two possibilities,
            '1. the event starts after the peak ends,
            If EventStartTimeForLoopDay > PeakEndTimeForLoopDay Then
                OffPeak = OffPeak + EventEndTimeForLoopDay - EventStartTimeForLoopDay
            Else
            '2. the event is ongoing when the peak ends
                OffPeak = OffPeak + EventEndTimeForLoopDay - PeakEndTimeForLoopDay
            End If
        End If
        
    Next loopDay
    
End Function
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top