I cooked up a quick function to consistently calculate the date of Monday of either the current week, or the Monday of a given week of any valid date.
I coded it in USA format where Sunday is the first day of the week.
I could not find a way to grab the current date in the function declaration and have the function arg Optional. That would have been:
which raises a compiler error as indicated:
"Compiler error:
Constant expression required"
So unfortunately usually the usage for now must be:
And the error condition will be returned as:
Any suggestions how to make the arg optional?
I am toying with the idea of supporting an optional Monday start of the week to make it compatible with Europe.
Any input? TIA!
I coded it in USA format where Sunday is the first day of the week.
Code:
Public Function datetimeutils_MondayOfThisWeek(ByVal dtmToday As Date) As Date
On Error GoTo Err_datetimeutils_MondayOfThisWeek
Dim intWeekdayNum As Integer
intWeekdayNum = Weekday(dtmToday)
'Note: No need to define an error return value. "12:00:00 AM" will be returned
'as that is the default value of a Date datatype variable
Select Case intWeekdayNum
Case vbSunday
datetimeutils_MondayOfThisWeek = DateAdd("d", 1, dtmToday)
Case vbMonday
datetimeutils_MondayOfThisWeek = dtmToday
Case vbTuesday
datetimeutils_MondayOfThisWeek = DateAdd("d", -1, dtmToday)
Case vbWednesday
datetimeutils_MondayOfThisWeek = DateAdd("d", -2, dtmToday)
Case vbThursday
datetimeutils_MondayOfThisWeek = DateAdd("d", -3, dtmToday)
Case vbFriday
datetimeutils_MondayOfThisWeek = DateAdd("d", -4, dtmToday)
Case vbSaturday
datetimeutils_MondayOfThisWeek = DateAdd("d", -5, dtmToday)
Case Else
Call errorhandler_MsgBox("Module: modshared_datetimeutils, Function: datetimeutils_MondayOfThisWeek(), Error: Unsupported intWeekdayNum value of: " & intWeekdayNum)
GoTo Exit_datetimeutils_MondayOfThisWeek
End Select
Exit_datetimeutils_MondayOfThisWeek:
Exit Function
Err_datetimeutils_MondayOfThisWeek:
Call errorhandler_MsgBox("Module: modshared_datetimeutils, Function: datetimeutils_MondayOfThisWeek()")
Resume Exit_datetimeutils_MondayOfThisWeek
End Function
Code:
Public Function datetimeutils_MondayOfThisWeek(Optional ByVal dtmToday As Date = [B][COLOR=Red]Date[/COLOR][/B]) As Date
"Compiler error:
Constant expression required"
So unfortunately usually the usage for now must be:
Code:
? datetimeutils_MondayOfThisWeek(Date)
11/5/2012
Code:
? datetimeutils_MondayOfThisWeek(Date)
12:00:00 AM
I am toying with the idea of supporting an optional Monday start of the week to make it compatible with Europe.
Any input? TIA!