previous Sundays date in visual basic

  • Thread starter Thread starter KL
  • Start date Start date
K

KL

Can anyone pls help with calculating the previous Sundays date in
visual basic, I need this as a default value in text box. Thanks.
 
This function should do what you seek:

Public Function DateOfSpecificWeekDay(ByVal OriginalDate As Date, _
ByVal intWeekDay As Integer) As Date
' ** THIS FUNCTION RETURNS THE DATE OF THE SPECIFIC DAY OF THE WEEK
' ** IN WHICH THE ORIGINAL DATE IS.
' ** intWeekDay = 1 is Sunday, 2 is Monday, etc.

On Error Resume Next

DateOfSpecificWeekDay = DateAdd("d", -DatePart("w", OriginalDate, _
1) + intWeekDay, OriginalDate)
Err.Clear
End Function
 
Thank you very much Ken, it does what I need.

This function should do what you seek:

Public Function DateOfSpecificWeekDay(ByVal OriginalDate As Date, _
ByVal intWeekDay As Integer) As Date
' ** THIS FUNCTION RETURNS THE DATE OF THE SPECIFIC DAY OF THE WEEK
' ** IN WHICH THE ORIGINAL DATE IS.
' ** intWeekDay = 1 is Sunday, 2 is Monday, etc.

On Error Resume Next

DateOfSpecificWeekDay = DateAdd("d", -DatePart("w", OriginalDate, _
1) + intWeekDay, OriginalDate)
Err.Clear
End Function
 
Have you tried?

WeekStart: DateAdd("d",1-Weekday(Date()),Date)
WeekEnd: DateAdd("d",7-Weekday(Date()),Date())

These should work as long as Sunday is set as the first day of the week in
your system settings.
 
Thanks John, Ken's code works as well.


John said:
Have you tried?

WeekStart: DateAdd("d",1-Weekday(Date()),Date)
WeekEnd: DateAdd("d",7-Weekday(Date()),Date())

These should work as long as Sunday is set as the first day of the week in
your system settings.
 
Back
Top