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

Datediff not including weekday?????

7 views
Skip to first unread message

kevinj...@gmail.com

unread,
Jun 6, 2006, 2:53:57 PM6/6/06
to
I am trying to get the date difference between two dates but I don't
want the function to include weekends in the calculation. Does anyone
have an idea on how to make this work?

CDMAP...@fortunejames.com

unread,
Jun 6, 2006, 3:12:32 PM6/6/06
to

Here is what I use to count weekend days:

'---Begin Code
Public Function CountWeekendDays(dtStart As Date, dtEnd As Date) As
Integer
Dim intSat As Integer
Dim intSun As Integer

'This function assumes dtStart <= dtEnd
CountWeekendDays = 0
intSat = DateDiff("d", GEDay(dtStart, 7), LEDay(dtEnd, 7)) / 7 + 1
intSun = DateDiff("d", GEDay(dtStart, 1), LEDay(dtEnd, 1)) / 7 + 1
CountWeekendDays = Ramp(intSat) + Ramp(intSun)
End Function

Public Function LEDay(dtX As Date, vbDay As Integer) As Date
LEDay = DateAdd("d", -(7 + WeekDay(dtX) - vbDay) Mod 7, dtX)
End Function

Public Function GEDay(dtX As Date, vbDay As Integer) As Date
GEDay = DateAdd("d", (7 + vbDay - WeekDay(dtX)) Mod 7, dtX)
End Function

Public Function Ramp(varX As Variant) As Variant
Ramp = IIf(Nz(varX, 0) >= 0, Nz(varX, 0), 0)
End Function
'---End Code

Sample Call:
MsgBox (CountWeekendDays(#3/1/06#, #5/1/06#))

18

Use the same dates you are using for your DateDiff in this function and
subtract the result from your DateDiff result.

James A. Fortune
CDMAP...@FortuneJames.com

kevinj...@gmail.com

unread,
Jun 6, 2006, 3:29:03 PM6/6/06
to
I am sure that works well in forms or macros, but will it work as an
expression in a query?

I should have noted, I am trying to do this through a query.

Sorry

kevinj...@gmail.com

unread,
Jun 6, 2006, 3:55:09 PM6/6/06
to
I found a solution that looks like it works in query functions

DateDiff('d',[test]![date1],[test]![date2],2)-(IIf(DateDiff('ww',[test]![date1],[test]![date2],2)=0,DateDiff('ww',[test]![date1],[test]![date2],2),(DateDiff('ww',[test]![date1],[test]![date2],2))*2))

Ron2006

unread,
Jun 6, 2006, 5:53:05 PM6/6/06
to
Similar to others, and does work in query

workdays
=DateDiff("d",[StartDate],[EndDate])-(DateDiff("ww",[StartDate],[EndDate],7)+DateDiff("ww",[StartDate],[EndDate],1))
+ 1

CDMAP...@fortunejames.com

unread,
Jun 7, 2006, 11:32:39 PM6/7/06
to

I have read here that using ww for some versions of Access doesn't
return the correct value for some dates. That's why I didn't use it.
Maybe someone can clarify which situations may be dangerous..

James A. Fortune
CDMAP...@FortuneJames.com

CDMAP...@fortunejames.com

unread,
Jun 8, 2006, 12:34:59 AM6/8/06
to
kevinj...@gmail.com wrote:
> I am sure that works well in forms or macros, but will it work as an
> expression in a query?
>
> I should have noted, I am trying to do this through a query.
>
> Sorry

If the code is placed in a module rather than behind a form then you
can call it from a query.

James A. Fortune
CDMAP...@FortuneJames.com

0 new messages