Getting current Fridays date

oxicottin

Learning by pecking away....
Local time
Today, 07:22
Joined
Jun 26, 2007
Messages
878
Hello I have the code below behind a button to get the current Fridays date. I tried it today which is Friday so it should give me todays date but it gives me next weeks Friday. What needs to be done in order to get the current Friday even if your on a Friday? Thanks!

Code:
Private Sub cmdCurrentFriday_Click()
    
    Dim dCurrentFriday As Date
    
    dCurrentFriday = DateAdd("d", 8 - Weekday(Date, vbFriday), Date)
    
    Me.txtWeekEnding = dCurrentFriday
    
    Me.txtMondaysDate.Requery
    Me.txtTuesdaysDate.Requery
    Me.txtWednsdaysDate.Requery
    Me.txtThursdaysDate.Requery
    Me.txtFridaysDate.Requery
    
End Sub
 
Test if you are on a Friday?
 
current friday:
Code:
'https://superuser.com/questions/376698/how-to-get-the-dates-of-the-current-monday-wednesday-and-friday-of-the-current
Public Function fnCurrentFridayOfDate(ByVal dte As Date) As Date
fnCurrentFridayOfDate = dte - Weekday(dte, 3) + 4
End Function

dCurrentFriday = fnCurrentFidayOfDate(Date())
 
current friday:
Code:
'https://superuser.com/questions/376698/how-to-get-the-dates-of-the-current-monday-wednesday-and-friday-of-the-current
Public Function fnCurrentFridayOfDate(ByVal dte As Date) As Date
fnCurrentFridayOfDate = dte - Weekday(dte, 3) + 4
End Function

dCurrentFriday = fnCurrentFidayOfDate(Date())
I have to ask @arnelgp, why Tuesday for the Weekday function?
 
I also tried ? date() - Weekday(dte, 3) + 4 in the immediate window and got 10/10/24. Would I change it to a Weekday(dte, 4)
 
you test whichever will work for you. as for the code, it works fine with my region.
 
I did the same. :(
We forgot to change the second dte. :-(

It works. :)
 
in fact i expanded the function to accept any Day of a week you need, if you need the current Monday of a certain date, just pass vbMonday as the second parameter, etc.
Code:
' current weekday of a date
'
Public Function fnCurrentWeekDayOfDate(ByVal dte As Date, ByVal iDay As VbDayOfWeek) As Date
Dim i As Integer
i = iDay - vbMonday
fnCurrentWeekDayOfDate = dte - Weekday(dte, 3) + i
End Function
 
Here's a sample with lots of useful date functions.

 
Hello I have the code below behind a button to get the current Fridays date. I tried it today which is Friday so it should give me todays date but it gives me next weeks Friday. What needs to be done in order to get the current Friday even if your on a Friday? Thanks!

Code:
Private Sub cmdCurrentFriday_Click()
   
    Dim dCurrentFriday As Date
   
    dCurrentFriday = DateAdd("d", 8 - Weekday(Date, vbFriday), Date)
   
    Me.txtWeekEnding = dCurrentFriday
   
    Me.txtMondaysDate.Requery
    Me.txtTuesdaysDate.Requery
    Me.txtWednsdaysDate.Requery
    Me.txtThursdaysDate.Requery
    Me.txtFridaysDate.Requery
   
End Sub
I presume your code needs to test whether today is Friday first.

I think you are adding 8 less today's day count, so you are always going to return a future date, as the dateadd evaluation is bound to find a date in the future.
 

Users who are viewing this thread

Back
Top Bottom