{"id":2917,"date":"2018-04-04T14:59:00","date_gmt":"2018-04-04T14:59:00","guid":{"rendered":"https:\/\/www.mjtnet.com\/blog\/?p=2917"},"modified":"2018-04-04T14:59:00","modified_gmt":"2018-04-04T14:59:00","slug":"get-date-of-last-monday","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2018\/04\/04\/get-date-of-last-monday\/","title":{"rendered":"Get Date of Last Monday"},"content":{"rendered":"<p>Someone asked me the other day how to get the date of the most recent Monday.\u00a0 I came up with two solutions.<\/p>\n<h2>Get Date of Last Monday with VBScript<\/h2>\n<p>This version works by looping the date backwards one day at a time until the weekday of that date is a Monday.<\/p>\n<pre class=\"brush:macroscript mb-3\">\r\n\/\/Put this VBSTART ... VBEND block at the top of your code ONCE\r\nVBSTART\r\n  Function LastMonday\r\n    LastMonday = Date()\r\n    Do While WeekDay(LastMonday) <> 2\r\n      LastMonday = LastMonday -1\r\n    Loop\r\n  End Function\r\nVBEND\r\n\r\n\/\/use this line whenever you want to get the date of last monday\r\nVBEval>LastMonday,dateOfLastMonday\r\n<\/pre>\n<p><\/p>\n<p>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.<\/p>\n<h2>Get Date of Last Monday with MacroScript<\/h2>\n<p>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&#8217;s a Sunday we just deduct 6. If it&#8217;s a Monday or onward we deduct the current day number.<\/p>\n<pre class=\"brush:macroscript mb-3\">\r\n\/\/DayOfWeek starts with 1 as Sunday, so Monday is 2.\r\nIf>curDayOfWeek>1\r\n  \/\/today must be Monday or later ... \r\n  \/\/So diff we want is 2 - current day of week\r\n  Let>diffToMonday=2-curDayOfWeek\r\n  \/\/e.g. if today Monday result would be 0 difference, Tuesday would yield -1 difference, Wednesday, -3 etc\r\nElse\r\n  \/\/curDayOfWeek is 1, so ...\r\n  \/\/today must be sunday, so we just want 6 days ago\r\n  Let>diffToMonday=-6\r\nEndif\r\n\r\n\/\/This should give us number of days to subtract from current day \r\n\/\/to get Monday's date\r\nDateAdd>today,D,%diffToMonday%,mbeg<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone asked me the other day how to get the date of the most recent Monday.\u00a0 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 &#8230; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2917"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=2917"}],"version-history":[{"count":4,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2917\/revisions"}],"predecessor-version":[{"id":2921,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2917\/revisions\/2921"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=2917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=2917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=2917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}