Between Two Dates - Calculate Hours/Minutes for Weekends if Applicable?

lager1001

Board Regular
Joined
May 17, 2019
Messages
88
I have two dates: START DATE/TIME and END DATE/TIME. I need to determine how many hours/minutes between these two values are exclusive to Saturdays and Sundays ONLY.

For a simple example: START DATE/TIME: 07/10/2020 12:00PM END DATE/TIME: 07/13/2020 12:00PM.

This started on a Friday at noon and ended on a Monday at noon so 48 hours of Saturday and Sunday would be in this range.

Next Example:

START DATE/TIME: 07/11/2020 8:00AM END DATE/TIME: 07/13/2020 10:00AM

This started on Saturday and ended on a Monday. so this has 40 hours falling on Saturday and Sunday within the range.

Can anyone assist?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Thanks for the article. I've looked at it and several others online but I'm not getting the results I need. I need this calculated down to the minute. My example above is in whole hours but minutes really matter here. If a Saturday or Sunday falls within the date range I need to exclude the hours and minutes for any time in the date/time duration that fell on Saturday or Sunday. For instance, there are several dates in the table that Start or End on a Saturday or Sunday, so it's very broken up and may only include a few hours of Saturday or Sunday in the range. I will keep researching. Thank you.
 
Upvote 0
Hi lager
Try this code below (i think it will meet your expectations.
Source TableResult
START DATE/TIMEEND DATE/TIMESTART DATE/TIMEEND DATE/TIMESatSun duration
2020-07-11 10:32:152020-07-13 17:45:272020-07-11 10:322020-07-13 17:4537:27:45
2020-07-11 10:32:152020-07-11 17:45:272020-07-11 10:322020-07-11 17:457:13:12
2020-07-11 10:32:152020-07-12 17:45:272020-07-11 10:322020-07-12 17:4531:13:12
2020-07-12 17:45:272020-07-19 17:40:312020-07-12 17:452020-07-19 17:4047:55:04
2020-07-12 17:45:272020-07-20 17:40:312020-07-12 17:452020-07-20 17:4054:14:33
2020-07-12 17:45:272020-07-18 13:20:122020-07-12 17:452020-07-18 13:2019:34:45
2020-07-13 17:45:272020-07-17 13:20:122020-07-13 17:452020-07-17 13:200:00:00
2020-07-11 13:30:052020-07-29 14:23:072020-07-11 13:302020-07-29 14:23130:29:55


Rich (BB code):
let
   fnSatSun = (s as datetime, e as datetime) => 
      let
         fnCount = (lst as list) => 
            let
               NsDate = Number.From(StartDate),
               NeDate = Number.From(EndDate),
               Transform = List.Transform(
                  lst, 
                  each 
                     if StartDate = EndDate
                     then DateTime.Time(e) - DateTime.Time(s)
                     else 
                        if NsDate = _
                        then Duration.From(1 - Number.From(DateTime.Time(s)))
                        else 
                           if NeDate = _
                           then Duration.From(Number.From(DateTime.Time(e)))
                           else #duration(1, 0, 0, 0)
               ),
               Total = List.Sum(Transform)
            in
               Total,

         StartDate = DateTime.Date(s),
         EndDate = DateTime.Date(e),
         LST = {Number.From(StartDate)..Number.From(EndDate)},
         SatSun = List.Select(LST, each Number.Mod(_, 7) < 2),
         Count = List.Count(SatSun),
         Result = if Count = 0 then #duration(0, 0, 0, 0) else fnCount(SatSun)
      in
         Result,

   Source = Excel.CurrentWorkbook(){[Name = "TimeTbl"]}[Content],
   #"Changed Type" = Table.TransformColumnTypes(
      Source, 
      {{"START DATE/TIME", type datetime}, {"END DATE/TIME", type datetime}}
   ),
   Result = Table.AddColumn(
      #"Changed Type", 
      "SatSun duration", 
      each fnSatSun([#"START DATE/TIME"], [#"END DATE/TIME"])
   )
in
   Result
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,121
Members
449,066
Latest member
Andyg666

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