Does someone know how to find the date for the next Friday based on the current date?
Rev. Michael L. Burns
Printable View
Does someone know how to find the date for the next Friday based on the current date?
Rev. Michael L. Burns
How aboutVB Code:
Private Sub Command1_Click() Dim FridaysDate As Variant Select Case Weekday(Now) Case 1 FridaysDate = Now + 5 Case 2 FridaysDate = Now + 4 Case 3 FridaysDate = Now + 3 Case 4 FridaysDate = Now + 2 Case 5 FridaysDate = Now Case 6 FridaysDate = Now + 7 Case 7 FridaysDate = Now + 6 End Select FridaysDate = Format(FridaysDate, "mm/dd/yyyy") MsgBox FridaysDate End Sub
VB Code:
Dim blnFriday as Boolean Dim dtDay as Date dtDay = Now + 1 Do If Format(dtDay, "dddd") = "Friday" Then MsgBox "Next Friday is " & dtDate blnFriday = True Else dtDay = dtDay + 1 blnFriday = False End If Loop Until blnFriday = True
No need to loop or to use Select Case
VB Code:
Public Function NextFriday() As Date Dim intDay As Integer 'if the toaday's weekday is more then Friday, then calculate 'number of days until next Friday, otherwise, get the difference between today and Next Friday intDay = IIf(WeekDay(Date) > vbFriday, vbSaturday - vbFriday, vbFriday - WeekDay(Date)) NextFriday = DateAdd("d", intDay, Date) End Function
Hack, seoptimizer2001, and Serge,
Thanks for the assitance. With the exception of one typeo in seoptimizer2001 response (dtDate instead of dtDay), all three solutions work great.
I also found a routine hidden deep within Total VB Sourcebook 6 that when used can determine any day of the week after a given date.
Public Function NextDOW(datDateIn As Date, intDay As Integer) As Date
' Comments : Returns the specified day of the week after the given
' date (eg. NextDOW (#03/28/64#, 6) returns the date of
' the next friday after 03/28/64)
' Parameters: datDateIn - date to check
' intDay - day of week to calculate (1=Sunday, 7=Saturday)
' Returns : date
' Source : Total VB SourceBook 6
'
On Error GoTo PROC_ERR
NextDOW = datDateIn - WeekDay(datDateIn) + intDay + _
IIf(WeekDay(datDateIn) < intDay, 0, 7)
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"NextDOW"
Resume PROC_EXIT
End Function
Usage Example:
Dim datTest As Date
datTest = "01/15/2002"
Debug.Print "The next Friday after " & datTest & " is " & _
NextDOW(datTest, 6)
Rev. Michael L. Burns
VB Code:
Public Function NextDOW(datDateIn As Date, intDay As Integer) As Date ' Comments : Returns the specified day of the week after the given ' date (eg. NextDOW (#03/28/64#, 6) returns the date of ' the next friday after 03/28/64) ' Parameters: datDateIn - date to check ' intDay - day of week to calculate (1=Sunday, 7=Saturday) ' Returns : date ' Source : Total VB SourceBook 6 ' On Error GoTo PROC_ERR NextDOW = datDateIn - WeekDay(datDateIn) + intDay + _ IIf(WeekDay(datDateIn) < intDay, 0, 7) PROC_EXIT: Exit Function PROC_ERR: MsgBox "Error: " & Err.Number & ". " & Err.Description, , _ "NextDOW" Resume PROC_EXIT End Function
Usage Example:
VB Code:
Dim datTest As Date datTest = "01/15/2002" Debug.Print "The next Friday after " & datTest & " is " & _ NextDOW(datTest, 6)
makes my life easier :)
Sorry Peet. With this thick skull of mine ya lost me here. Can you please explain?
Pastor Mike
Rev, check the link in my signature, but instead of the code or php tags use vbcode tags. I know they aren't listed, but they do work. It just makes code easier to read.:)
sorry.. should have explained it better :)Quote:
Originally posted by Rev. Michael L. Burns
Sorry Peet. With this thick skull of mine ya lost me here. Can you please explain?
Pastor Mike
when typing/pasting code, place it inbetween vbcode tags like this :
[vbcode]
Dim s As String
s = "ABC"
MsgBox s
[/vbcode]
it will when u post it look like this:
VB Code:
Dim s As String s = "ABC" MsgBox s
makes it a lot easier to read and understand :)
Thanks Peet. I always wondered how that was done but never took the time to investegate. Will try to use it in the future.
Pastor Mike