Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to: Calculate # weekdays between two dates...excluding weekends

941 views
Skip to first unread message

Roz

unread,
Jun 2, 2003, 8:40:30 PM6/2/03
to
Hello, all. I'm trying to figure out how to calculate the
number of WEEKDAYS between two dates. The DateDiff
function won't work because it takes into account weekend
days. I need to exclude weekend days (Sat and Sun). I
only need to know the number of WEEKDAYS between Date A
and Date B.

And tips, code, ideas, etc would be most appreciated.
TIA
Roz


Ken Schaefer

unread,
Jun 2, 2003, 9:28:01 PM6/2/03
to
a) Use DateDiff() to find out how many days there are
b) Use WeekDay() on the first date, to find out what day of the week it is
c) Using the results from (a) and (b) you should be able to work out when
the next weekend day is. Then, each 7th day after that should be the start
of a weekend, up until the last day. Use a loop to work out how many
weekends there are.

Cheers
Ken


"Roz" <ros...@nospam.swbell.net> wrote in message
news:28SCa.1356$9L6...@newssvr32.news.prodigy.com...
: Hello, all. I'm trying to figure out how to calculate the

:
:


Don Grover

unread,
Jun 2, 2003, 11:37:38 PM6/2/03
to
Here ya go, ripped it out of one of my old vb app's and converted it.

<%
datefrom = date
dateto = date - 100

Response.write "Business Days: " & BusinessDateDiff(datefrom,dateto)


Public Function BusinessDateDiff( Date1 , Date2 )

Dim iWeekday1
Dim iWeekday2
Dim lDateDiff
Dim lWeeks
Dim dTemp

If Date1 > Date2 Then
dTemp = Date1
Date1 = Date2
Date2 = dTemp
End If

iWeekday1 = WeekDay(Date1, vbMonday) - 1
iWeekday2 = WeekDay(Date2, vbMonday) - 1
If iWeekday1 > 4 Then iWeekday1 = 4
If iWeekday2 > 4 Then iWeekday2 = 4

lWeeks = DateDiff("w", Date1, Date2)

If iWeekday1 <= iWeekday2 Then
lDateDiff = (lWeeks * 5) + (iWeekday2 - iWeekday1)
Else
lDateDiff = ((lWeeks + 1) * 5) - (iWeekday1 - iWeekday2)

End If

BusinessDateDiff = lDateDiff

End Function
%>


"Roz" <ros...@nospam.swbell.net> wrote in message
news:28SCa.1356$9L6...@newssvr32.news.prodigy.com...

dlbjr

unread,
Jun 2, 2003, 11:42:05 PM6/2/03
to
<SCRIPT language="vbscript" runat="server">
Function GetWeekDays(dtmStart,dtmEnd)
GetWeekDays = "Error"
If IsDate(dtmStart) And IsDate(dtmEnd) Then
If CDate(dtmStart) > CDate(dtmEnd) Then
dtmTemp = dtmStart
dtmStart = dtmEnd
dtmEnd = dtmTemp
End If
intDayOfWeek = DatePart("w",dtmStart)
intDays = DateDiff("d",dtmStart,dtmEnd)
intWeekCount = intDays \ 7
GetWeekDays = (intWeekCount * 5)
For intCount = 0 To intDays - (intWeekCount * 7)
intDayOfWeek = DatePart("w",DateAdd("d",intCount + (intWeekCount * 7),dtmStart))
If intDayOfWeek > 1 And intDayOfWeek < 7 Then
GetWeekDays = GetWeekDays + 1
End If
Next
End If
End Function
</SCRIPT>
<%
Response.Write GetWeekDays("5/1/03","6/10/03")
%>

-------------------------------------------------
d l b j r

Unambit from meager knowledge of inane others,
engender uncharted sagacity.
-------------------------------------------------


Roz

unread,
Jun 3, 2003, 9:03:57 AM6/3/03
to
Thanks guys. This helps alot!

Roz

>.
>

Don Verhagen

unread,
Jun 3, 2003, 9:15:23 AM6/3/03
to
In news:uqEnh9WK...@TK2MSFTNGP10.phx.gbl,
Ken Schaefer <kenR...@THISadOpenStatic.com> typed:
: a) Use DateDiff() to find out how many days there are

: b) Use WeekDay() on the first date, to find out what day of the week
: it is c) Using the results from (a) and (b) you should be able to
: work out when the next weekend day is. Then, each 7th day after that
: should be the start of a weekend, up until the last day. Use a loop
: to work out how many weekends there are.

I see this question (or the like) frequently in here that involve relatively
simple math. Remember that kid in high school Alegbra who said, "When am I
ever going to use this?". Answer: When you become a programmer or scalable
website designer.

:
: Cheers

0 new messages