Working out weekdays in a query - one date away from another but only weekdays. (1 Viewer)

NickPlymouth

New member
Local time
Today, 05:21
Joined
Jul 22, 2022
Messages
3
Hi Guys, its been about 3 years since I last logged in, so hello and I hope you are all well. I am trying to work out how many days between two dates but only count weekdays (I also need to take out UK holidays but that can wait). I have looked on YT as you do and no matter what I am doing it still will not work, errors each time I run it. I can work out the exact days between two dates just by taking one away from another but not as weekdays only. Any assistance would be most welcome. Thank you. Sorry if you think this is beginners stuff, but I rarely do reports these days from queries as they are all set up. Regards, Nick.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:21
Joined
May 7, 2009
Messages
19,247
you can create a function that count only the workdays:
Code:
public function fnCountWorkDays(byval dtStart As Date, dtEnd As Date) As Integer
' weekends are Saturday and Sunday
dim dt As Date, cnt As Integer
for dt = dtStart To dtEnd
    If Instr(1, "Sat/Sun", Format$(dt, "ddd")) = 0 then
        cnt = cnt + 1
    end if
next
fnCountWorkDays = cnt
end function

'example to count workdays in the month of january (the date is in EN-US format):
Dim workDays As Integer
workDays = fnCountWorkDays(#1/1/2022#, #1/31/2022#)
 

NickPlymouth

New member
Local time
Today, 05:21
Joined
Jul 22, 2022
Messages
3
you can create a function that count only the workdays:
Code:
public function fnCountWorkDays(byval dtStart As Date, dtEnd As Date) As Integer
' weekends are Saturday and Sunday
dim dt As Date, cnt As Integer
for dt = dtStart To dtEnd
    If Instr(1, "Sat/Sun", Format$(dt, "ddd")) = 0 then
        cnt = cnt + 1
    end if
next
fnCountWorkDays = cnt
end function

'example to count workdays in the month of january (the date is in EN-US format):
Dim workDays As Integer
workDays = fnCountWorkDays(#1/1/2022#, #1/31/2022#)
OH MY GOODNESS, it worked first time. Thank you arnelgp, you are amazing. I learnt how to write queries and reports all on my own a long time ago, was never shown anything. I cannot thank you enough. Bravo.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:21
Joined
May 7, 2009
Messages
19,247
you can also use this shortcut function:
Code:
Public Function fnCountWorkDays(ByVal dtStart As Date, dtEnd As Date) As Integer
' weekends are Saturday and Sunday
fnCountWorkDays = DateDiff("d", dtStart, dtEnd, vbSunday) + 1 - _
                  DateDiff("ww", dtStart, dtEnd, vbSaturday) - _
                  DateDiff("ww", dtStart, dtEnd, vbSunday)
End Function
 

NickPlymouth

New member
Local time
Today, 05:21
Joined
Jul 22, 2022
Messages
3
you can also use this shortcut function:
Code:
Public Function fnCountWorkDays(ByVal dtStart As Date, dtEnd As Date) As Integer
' weekends are Saturday and Sunday
fnCountWorkDays = DateDiff("d", dtStart, dtEnd, vbSunday) + 1 - _
                  DateDiff("ww", dtStart, dtEnd, vbSaturday) - _
                  DateDiff("ww", dtStart, dtEnd, vbSunday)
End Function
Thank you so much, you must be a MS Access Guru. Best Wishes, Nick in Plymouth.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,343
Arnel
Hi Guys, its been about 3 years since I last logged in, so hello and I hope you are all well. I am trying to work out how many days between two dates but only count weekdays (I also need to take out UK holidays but that can wait). I have looked on YT as you do and no matter what I am doing it still will not work, errors each time I run it. I can work out the exact days between two dates just by taking one away from another but not as weekdays only. Any assistance would be most welcome. Thank you. Sorry if you think this is beginners stuff, but I rarely do reports these days from queries as they are all set up. Regards, Nick.
I think you might as well supply your holiday version now Arnel? :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:21
Joined
Feb 19, 2002
Messages
43,331
Here's one that has a bunch of useful date functions.

 

Cotswold

Active member
Local time
Today, 05:21
Joined
Dec 31, 2020
Messages
528
More date stuff.

I often fiddle about with string handling in all sorts of situations.
However, before I'm deluged with examples of all the alternatives, I just want to say that I do actually know there are several other ways of doing it. But I prefer this one. Just two lines and it's all there to see so clearly. Use it on a Form, or stick it in a Function and use it anywhere.

If you want to display today as 25th Jul 2022 then these two lines do it.

Code:
mVarDaySfx = Mid("stndrdthththththththththththththththththstndrdthththththththst", (((Day(Date) - 1 * 2) * 2) + 1), 2)
mVarDateToday = CStr(Day(Date)) + mVarDaySfx + " " + Mid("JanFebMarAprMayJunJulAugSepOctNovDec", (((Month(Date) - 1) * 3) + 1), 3) + " " + CStr(Year(Date))
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,343
Where does mDaySfx come from?
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,343
Carelessness but I'll call it a typo! The variables I used were maybe not so clear, so I altered them in the posting
I knew that. :)
It was really for the benefit of those people who just copy and paste, without any regard to what the code says or does. :)

Otherwise someone is gong to come back and say 'It does not work' :)
 

Users who are viewing this thread

Top Bottom