473,791 Members | 3,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datediff not including weekday?????

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?

Jun 6 '06 #1
6 7650
ke**********@gm ail.com wrote:
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?


Here is what I use to count weekend days:

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

'This function assumes dtStart <= dtEnd
CountWeekendDay s = 0
intSat = DateDiff("d", GEDay(dtStart, 7), LEDay(dtEnd, 7)) / 7 + 1
intSun = DateDiff("d", GEDay(dtStart, 1), LEDay(dtEnd, 1)) / 7 + 1
CountWeekendDay s = 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 (CountWeekendDa ys(#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
CD********@Fort uneJames.com

Jun 6 '06 #2
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

CD********@Fort uneJames.com wrote:
ke**********@gm ail.com wrote:
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?


Here is what I use to count weekend days:

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

'This function assumes dtStart <= dtEnd
CountWeekendDay s = 0
intSat = DateDiff("d", GEDay(dtStart, 7), LEDay(dtEnd, 7)) / 7 + 1
intSun = DateDiff("d", GEDay(dtStart, 1), LEDay(dtEnd, 1)) / 7 + 1
CountWeekendDay s = 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 (CountWeekendDa ys(#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
CD********@Fort uneJames.com


Jun 6 '06 #3
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))

ke**********@gm ail.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

CD********@Fort uneJames.com wrote:
ke**********@gm ail.com wrote:
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?


Here is what I use to count weekend days:

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

'This function assumes dtStart <= dtEnd
CountWeekendDay s = 0
intSat = DateDiff("d", GEDay(dtStart, 7), LEDay(dtEnd, 7)) / 7 + 1
intSun = DateDiff("d", GEDay(dtStart, 1), LEDay(dtEnd, 1)) / 7 + 1
CountWeekendDay s = 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 (CountWeekendDa ys(#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
CD********@Fort uneJames.com


Jun 6 '06 #4
Similar to others, and does work in query

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

Jun 6 '06 #5
Ron2006 wrote:
Similar to others, and does work in query

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


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
CD********@Fort uneJames.com

Jun 8 '06 #6
ke**********@gm ail.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
CD********@Fort uneJames.com

Jun 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1845
by: Tim::. | last post by:
I am having some difficulties with the function Datediff! I am trying to calculate the number of days between two given date E.G: Number days between 03-05-2004 and 05-05-2004 = I am using the following piece of asp to extract dates from an XML document and work with them to create the above mentioned result, however I can't get it to work Can someone please assist! Thank
4
11918
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS STARTINGDATE1 ENDINGDATE1 STARTINGDATE2
5
6692
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than Access. I have a database where they customer wants to purge records from certain tables after three years. When I was asked to make the changes I originally thought this was an incredibly simple thing to do. I looked at the function list and...
3
6941
by: haydn_llewellyn | last post by:
Hi, My company runs on a fiscal calendar that starts on the first monday in July, and is based on a 13 week quarter (4 weeks, 4 weeks, 5 weeks). What I need, is a way of relating Date() to the actual fiscal month (e.g. today is 01/07/05, and is the last day in June in our fiscal calendar). I tried many methods, and had hoped that DatePart() with the 'ww' flag and DatePart() with the 'w' flag (to choose the first monday in July,
1
2030
by: Kd | last post by:
I am currently using a form with Weekdays Mon Tues Wed Thur Fri This is generated by a table that the days are being entered manually I would like to create a form that the days updated themselves by 7 days each time a new record is opened. Any and all thought are appreciated
3
13431
by: Price Brattin | last post by:
Why is the DateDiff function in the following code returning zero? Dim FileDate, TransmissionDate as Date Dim TranDay, FileDay, DayDiff as Inteter TransmissionDate = #2/5/2006 1:57:56 PM# FileDate = #2/4/2006 4:49:02 PM# TranDay = DatePart(DateInterval.DayOfYear, TransmissionDate) 'Returns 36 FileDay = DatePart(DateInterval.DayOfYear, FileDate) 'Returns 35 DayDiff = DateDiff(DateInterval.DayOfYear, FileDate, TransmissionDate) 'Returns...
4
6704
by: dgmoore | last post by:
I've hit a snag - I know this is easy, but the logic is escaping me. I need to set criteria in a query to find dates in a date field that lie between Tuesday of the current week and Tuesday of the previous week. Any suggestions would be appreciated. Thanks Dave
0
1627
by: Rudi Hausmann | last post by:
Hi! I have a column with a date. In another column I want to show the weekday of this date. I have following code: <asp:BoundField DataField="myDate" HeaderText="WeekDay" SortExpression="weekday" DataFormatString="{0:dddd}" HtmlEncode="False" ApplyFormatInEditMode="True" /> <asp:BoundField DataField="myDate" HeaderText="Day" SortExpression="weekday" DataFormatString="{0:d}" HtmlEncode="False"
4
5143
ddtpmyra
by: ddtpmyra | last post by:
I have below query inside the ms access, but this query counts all the days including the weekend but I only wanted to count the NUMBER of weekday (Monday to Friday). Please help Number of Days Open: DateDiff('d',!,Date()) I tried to us the 'w' and 'ww' but it counts the number week not the number of days in a week. Number of Days Open: DateDiff('w',!,Date()) Thanks!
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.