April 4, 2018

Filed under: Scripting — Marcus Tettmar @ 2:59 pm

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
  Function LastMonday
    LastMonday = Date()
    Do While WeekDay(LastMonday) <> 2
      LastMonday = LastMonday -1
  End Function

//use this line whenever you want to get the date of last monday

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.
  //today must be Monday or later ... 
  //So diff we want is 2 - current day of week
  //e.g. if today Monday result would be 0 difference, Tuesday would yield -1 difference, Wednesday, -3 etc
  //curDayOfWeek is 1, so ...
  //today must be sunday, so we just want 6 days ago

//This should give us number of days to subtract from current day 
//to get Monday's date

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment