weekday or weeken

XaloRichie

Registered User.
Local time
Today, 02:18
Joined
Jul 2, 2003
Messages
70
I have a form where the user eneters 2 dates.

I want to beable to check if the dates are mon-fri (making weekday) or sat & Sun making weekend. Well I also need to know if the period starts on say friday and ends on monday. so i can prompt user that a weekend is included.

If anyone has a cool example it would be great?
 
You can use the weekday() function to return what day it is.

Fuga.
 
Thank you but there's more

The Weekday function has worked well and having looked at a few examples on other threads I have it calculating how many week days and how many weekend days between the start and finish dates.

Now in my DB there is a price for a week (being 5 days mon-fri)
and a different price for both Weekends and single days.'

So for instance I might have 6 weekdays and one weekend
Which I need to split like this.
5days=weekcharge +2weekenddays=1weekend charge + 1 Singleday @ daycharge.

The thing is if i divide my weekdays by 5 eg. 6/5 =1.2 and I need a way to count the .2 into a single day? and obviously I could have n weekdays over a longer period and so would need to cope with say 23 weekdays = 4 weeks and 3 single days. Of course dividing by 5 = 4.6 So how can i convert that .6 to be 3 days?
Do you see what I am trying to do? Im thinking i need to grab the remainder and manipulate it.

Thanks again all'
Rich
 
WOW Just Found Mod()

Is this reliable for this 8 Mod 5 =3 which would be correct if 5 days = 1 wk then mod gives me the remainding days????

Can it be true?
 
Heres a function I made. I pass on Now() bu you should be able to pass on what the user has entered.

I use it to always set the date to the following monday but you can modify it to your needs to see if it is the weekend or not.

Code:
Function SetMonDate(CurrentDate)

   If VarType(CurrentDate) <> 7 Then
      SetMonDate = Null
   Else
      Select Case Weekday(CurrentDate)
         Case 1      ' Sunday
            SetMonDate = CurrentDate + 1
         Case 2      ' Monday
            SetMonDate = CurrentDate + 7
         Case 3      ' Tuesday
            SetMonDate = CurrentDate + 6
         Case 4      ' Wednesday
            SetMonDate = CurrentDate + 5
         Case 5      ' Thursday
            SetMonDate = CurrentDate + 4
         Case 6      ' Friday
            SetMonDate = CurrentDate + 3
         Case 7      ' Saturday
            SetMonDate = CurrentDate + 2
      End Select
   End If
   
   Me!txtDate.value = Format(SetMonDate, "mm/dd/yyyy")
   
End Function
 
this is what i have so far

countA = DateDiff("d", Forms![rettool1]![StDate], Date, vbMonday, vbUseSystem)

For intcount = 0 To countA
If Weekday(Strdatestart + intcount) < 6 Then
MsgBox "<6 so + wd" & Weekday(Strdatestart + intcount)
wdcount = wdcount + 1
MsgBox "weekday" & wdcount
End If

If Weekday(Strdatestart + intcount) > 5 Then
MsgBox ">5 so add we" & Weekday(Strdatestart + intcount)
wecount = wecount + 1
MsgBox "" & wecount
End If

Next intcount
------------------------------------------
But with startdate of 18/09/03 (thurs) to 25/09/03 (Thurs)
I am getting 5week days and 3 weekend days ?? which aint right.
i should be getting 6 weekdays and 2 weekend days!

What have i done ??
 
Still need help

Code
-----------------------
countA = DateDiff("d", Forms![rettool1]![StDate], Date, VbMonday, vbUseSystem)
For intcount = 0 To countA
MsgBox "count" & intcount
If Weekday(Strdatestart + intcount) < 6 Then wdcount = wdcount + 1
If Weekday(Strdatestart + intcount) > 5 Then wecount = wecount + 1
Next intcount

MsgBox "Actual Hire " & wdcount & " weekdays and " & wecount & " Weekend days"
------------------------------------------------------------------
The above code works except it counts 3 weekend days in a period of 8 days????

Any Suggestions?
 
Re: WOW Just Found Mod()

XaloRichie said:
Is this reliable for this 8 Mod 5 =3 which would be correct if 5 days = 1 wk then mod gives me the remainding days????

Can it be true?

Mod is not a function that works with dates...mod returns as an integer the remainder of integer division.

For example,

8 divided by 5 is really 1 with a remainder of 3. there is nothing wrong with the Mod operator.

What are you trying to do maybe I can help...

Jon
 
Re: Still need help

Here's some good functions I've used:

Code:
Function getFirstDayOfMonth(month As Date) As Date
On Error GoTo Err_Handler
'returns the first day of a month

Dim strDate As String
strDate = DatePart("m", month, vbMonday) & "/" & "1/" & DatePart("yyyy", month, vbMonday)
getFirstDayOfMonth = CDate(strDate)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function
Function getFirstDayOfNextMonth(month As Date) As Date
On Error GoTo Err_Handler
'returns the first day of the next month
Dim strDate As String

strDate = DatePart("m", month, vbMonday) & "/" & "1/" & DatePart("yyyy", month, vbMonday)
strDate = DateAdd("m", 1, strDate)

getFirstDayOfNextMonth = CDate(strDate)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function
Function numberofweeks(date1 As Date, date2 As Date)
On Error GoTo Err_Handler
'returns the number of weeks between two dates
'
numberofweeks = DateDiff("ww", date1, date2, vbMonday, vbFirstJan1)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function
Public Function getnumofmonths(date1 As Date, date2 As Date)
On Error GoTo Err_Handler
'returns the number of months on two dates

getnumofmonths = DateDiff("m", getFirstDayOfMonth(date1), EofMonth(date2), vbMonday, vbFirstJan1)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function
Public Function weeknum(date1 As Date) As Integer
On Error GoTo Err_Handler
'returns the week number
Dim strYear As String

strYear = "01/01/"
strYear = strYear & Right(date1, 2)

'get the week number based on the incoming date
'of the year...
weeknum = DateDiff("ww", strYear, date1, vbMonday, vbFirstJan1)

'weeks start at 1 till 52...
If weeknum = 0 Then
    weeknum = weeknum + 1
Else
    'do nothing
End If

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function
Public Function monthname(date1 As Date, incre As Double) As Date
On Error GoTo Err_Handler

monthname = DateAdd("m", incre, date1)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function

Public Function EofMonth(date1 As Date) As Date
On Error GoTo Err_Handler

Dim myDate As Date

myDate = getFirstDayOfMonth(date1)
myDate = DateAdd("m", 1, myDate)

EofMonth = DateAdd("d", -1, myDate)

Done:
Exit Function

Err_Handler:
MsgBox Err.Description, vbCritical, "Error #: " & Err.Number
Resume Done

End Function

Jon
 
Re: Still need help

countA = DateDiff("d", Forms![rettool1]![StDate], Date, VbMonday, vbUseSystem)
For intcount = 0 To countA
MsgBox "count" & intcount
If Weekday(Strdatestart + intcount) < 6 Then wdcount = wdcount + 1
If Weekday(Strdatestart + intcount) > 5 Then wecount = wecount + 1
Next intcount

Dont you mean:

countA = DateDiff("d", Forms![rettool1]![StDate], Date, VbMonday, vbUseSystem)
For intcount = 0 To countA-1
MsgBox "count" & intcount
If Weekday(Strdatestart + intcount) < 6 Then wdcount = wdcount + 1
If Weekday(Strdatestart + intcount) > 5 Then wecount = wecount + 1
Next intcount
 
For a little different approach, try pasting the following sub and function to a new module, then running the sub:
Code:
Sub XaloRichie()
Dim pStartDte As Date
Dim pEndDte As Date
Dim x As Date
Dim strHold As String
Dim strAmt As String
Dim intHold As Integer
Dim i As Integer
Dim NL, fmt, msg

NL = Chr(13) & Chr(10)  ' Define newline.
fmt = "Medium Date"  ' Define date format.
msg = "Enter the start date (dd-mmm-yyyy)."
pStartDte = DateValue(InputBox(msg))
x = pStartDte
msg = "Enter the end date (dd/mmm/yyyy)."
pEndDte = DateValue(InputBox(msg))
strHold = "For the period: " & Format(pStartDte, fmt) & " through "
strHold = strHold & Format(pEndDte, fmt) & " there are: " & NL & NL
For i = 1 To 2
  pStartDte = x
  If i = 1 Then
     intHold = CalcWkDays3(pStartDte, pEndDte, True, "17")
     strHold = strHold & intHold \ 5 & " week(s)" & NL
     strHold = strHold & intHold Mod 5 & " single day(s)" & NL
  Else
     intHold = CalcWkDays3(pStartDte, pEndDte, True, "23456")
     strHold = strHold & intHold & " weekend day(s)" & NL
  End If
Next i

MsgBox Left(strHold, Len(strHold) - 1), vbOKOnly, "!"
End Sub

Function CalcWkDays3(dteStartDate As Date, dteEndDate As Date, _
YCnt As Boolean, Optional pExcl As String = "17") As Integer
'*****************************************************
'Name:      CalcWkDays3 (Function)
'Purpose:   Count # of days between two dates, with
'           options to:
'           (1) Include or exclude the start date in count
'           (2) Specify weekdays to exclude (default
'               set to Saturday (7) & Sunday (1).  To exclude
'               Tuesday (3) & Thursday (5), specify "35".
'Parameters:    dteStartDate & dteEndDate formatted as dates
'               YCnt:   Specify True to include start date in
'                       count, False to exclude it.
'               pExcl:  Weekdays to exclude
'Inputs:    From debug window:
'           (1) ? CalcWkDays3(#01/01/01#, #07/01/01#, True)
'           (2) ? CalcWkDays3(#01/01/01#, #07/01/01#, False)
'           (3) ? CalcWkDays3(#01/01/01#, #07/01/01#, True, "")
'           (4) ? CalcWkDays3(#01/01/01#, #07/01/01#, False,"")
'           (5) ? CalcWkDays3(#07/01/01#, #01/01/01#, False,"")
'           (6) ? CalcWkDays3(#01/01/01#, #07/01/01#, False,"23456")
'Output:    (1) 130; (2) 129; (3) 182; (4) 181; (5) -181; (6) 52
'
'*****************************************************

Dim n As Integer, wdays As String, datehold As Date, dteFlag As Boolean

dteFlag = False
'Reverse the dates if they were input backwards
If dteStartDate > dteEndDate Then
   datehold = dteStartDate
   dteStartDate = dteEndDate
   dteEndDate = datehold
   dteFlag = True
End If
n = 0
dteStartDate = dteStartDate - Not (YCnt)
'days to exclude (7 & 1 unless other specified)
wdays = pExcl

Do While dteStartDate <= dteEndDate
   n = n + IIf(InStr(wdays, WeekDay(dteStartDate)) = 0, 1, 0)
   dteStartDate = dteStartDate + 1
Loop
'return negative value if the Start Date
'was initially greater than the End Date
CalcWkDays3 = n * IIf(dteFlag, -1, 1)
End Function
 
Ahh...its good ol raskew...still doing vb/vba ehh?
Havent moved to bigger brighter things with that ego of yours :).
 
Jon-

What an unexpected surprise?!

Slipped off our meds, did we?

Perhaps when your therapist is back in town you'll post your complete solution to XaloRichie's dilemma.

Bob
 
Good to see you back grump...I see you haven't learned anything else but access :). Boy have things changed...
its always a good feeling to see others have moved on to bigger and better...how about some C++ and SQL Server big guy ;)
 
Don’t have a clue where you’re going with this.
Apparently somewhere along the line I’ve
managed to offend you.

I apologize and wish you the best with C++ and
SQL Server and whatever else you’re really into
these days. I’m sure you’re the greatest!

P.S.: Mom just called and asked that we remind
you that this is the weekend and to make sure
that you wash your gym shorts before you go to
your Star-Trek meeting.

“Mission2Java_78” – give us a break!

p.s.s: Still waiting for your posted solution!
 
Dying of laughter...

BTW your function is overkill for something so simple as DateDiff :)...but thats your time not mine!!!! Have fun with VBA!!!

Jon
 
Rich said:
I see the testimony on your "about us " page is still blank, jon:rolleyes:

Heh...

I do web stuff for the enjoyment...I actually work for the auto industry (part of it)...I worked on dbguy a while ago just to see what I could come up with with CCS (cascade sheets)...the idea was to see if my table layout would center around the menu..after I got it working I never put any content into it...its nice you visited though...where's your site?

Thanks,
 
Mr. Java,

I'm sure you're probably correct.

Just so that we can rest easy, please post your solution to the original enquiry.

Bob

p.s.: Rich, your response was right on target!!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom