Someone asked me the other day how to get the date of the most recent Monday. I came up with two solutions.
Get Date of Last Monday with VBScript
This version works by looping the date backwards one day at a time until the weekday of that date is a Monday.
//Put this VBSTART ... VBEND block at the top of your code ONCE VBSTART Function LastMonday LastMonday = Date() Do While WeekDay(LastMonday) <> 2 LastMonday = LastMonday -1 Loop End Function VBEND //use this line whenever you want to get the date of last monday VBEval>LastMonday,dateOfLastMonday
To make it work for a different day just change the number it compares the WeekDay to in the Do line. 1 is Sunday, 2 is Monday and so on.
Get Date of Last Monday with MacroScript
This version works out how many days we need to deduct from the current date to get to Monday based on what the current day of the week is. If it’s a Sunday we just deduct 6. If it’s a Monday or onward we deduct the current day number.
//DayOfWeek starts with 1 as Sunday, so Monday is 2. If>curDayOfWeek>1 //today must be Monday or later ... //So diff we want is 2 - current day of week Let>diffToMonday=2-curDayOfWeek //e.g. if today Monday result would be 0 difference, Tuesday would yield -1 difference, Wednesday, -3 etc Else //curDayOfWeek is 1, so ... //today must be sunday, so we just want 6 days ago Let>diffToMonday=-6 Endif //This should give us number of days to subtract from current day //to get Monday's date DateAdd>today,D,%diffToMonday%,mbeg