{"id":65453,"date":"2007-02-23T01:00:00","date_gmt":"2007-02-23T01:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/23\/how-can-i-tell-if-a-script-is-running-on-the-first-or-second-monday-of-a-month\/"},"modified":"2007-02-23T01:00:00","modified_gmt":"2007-02-23T01:00:00","slug":"how-can-i-tell-if-a-script-is-running-on-the-first-or-second-monday-of-a-month","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-script-is-running-on-the-first-or-second-monday-of-a-month\/","title":{"rendered":"How Can I Tell If a Script is Running on the First or Second Monday of a Month?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! We have a logon script that needs to perform certain tasks only on the first and second Mondays of the month. How can I determine whether the script is running on the first or second Monday in a month?<BR><BR>&#8212; WA<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, WA. You know, a few months ago the Scripting Guy who writes this column noticed that his watch had stopped; consequently, he went to the store and paid $20 to get a new battery. Less than two months later his watch stopped again. Could the battery have died after just two months? Maybe, but, then again, maybe his watch was truly broken. With that in mind the Scripting Guy who writes this column went back to the store and this time, rather than buy a new battery, he bought a new watch that was on sale for $9.95.<\/P>\n<P>Now, we know what you\u2019re thinking, you\u2019re thinking, \u201cWhy would one of the world-famous Scripting Guys wear a $9.95 watch? Shouldn\u2019t the Scripting Guys all wear Rolexes or something like that?\u201d <\/P>\n<P>No, they shouldn\u2019t. And here\u2019s why. Last night this Scripting Guy was boiling water in preparation for cooking some noodles. (Of <I>course<\/I>, the Scripting Guy who writes this column can cook. Any time you want boiling water for dinner just let him know.) As he reached across the stove to turn down the burner, his $9.95 watch somehow slipped off his arm and plopped into the pot of boiling water.<\/P>\n<P>Amazingly enough, the watch continued to work; being immersed in boiling water for a minute or so had no effect on it whatsoever. Try doing <I>that<\/I> with a Rolex!<\/P>\n<TABLE class=\"dataTable\" id=\"EID\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Never mind; whatever you do <I>don\u2019t<\/I> try doing that with a Rolex. Microsoft\u2019s insurance company probably won\u2019t be too happy if thousands of people suddenly start filing claims for new Rolex watches just because the Scripting Guys told them to try dunking those watches in boiling water.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what does any of that have to do with writing a script that can determine whether it\u2019s running on the first or second Monday of the month? As near as we can tell, nothing. But it you\u2019ve ever wondered what the Scripting Guy who writes this column is <I>really<\/I> like, well, now you know.<\/P>\n<TABLE class=\"dataTable\" id=\"E2D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. But please don\u2019t tell the other Scripting Guys about this incident. After all, they look at the Scripting Guy who writes this column as if he was Mary Poppins: practically perfect in every way.<\/P>\n<P><B>Editor\u2019s Note.<\/B> Please don\u2019t tell the Scripting Guy who writes this column that the other Scripting Guys know better. He\u2019s happy in his little fantasy world, we\u2019d hate to ruin that for him.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But don\u2019t despair, WA; the following chunk of code should help a bit more when it comes to writing a script that can determine whether it\u2019s running on the first or second Monday of the month:<\/P><PRE class=\"codeSample\">Dim arrMondays(1)<\/p>\n<p>dtmMonth = Month(Date)\ndtmYear = Year(Date)\ndtmDate = CDate(dtmMonth &amp; &#8220;\/1&#8221; &amp; &#8220;\/&#8221; &amp; dtmYear)<\/p>\n<p>Do Until i = 1\n    intWeekDay = Weekday(dtmDate)\n    If intWeekDay = 2 Then\n        arrMondays(0) = dtmDate\n        arrMondays(1) = dtmDate + 7\n        Exit Do\n    End If\n    dtmDate = dtmDate + 1\nLoop<\/p>\n<p>For Each strMonday in arrMondays\n    If Date = strMonday Then\n        Wscript.Echo &#8220;Carry out the task.&#8221;\n    Else\n        Wscript.Echo &#8220;Don&#8217;t carry out the task.&#8221;\n    End If\nNext\n<\/PRE>\n<P>So how does this script work? You <I>would<\/I> have to ask us that, wouldn\u2019t you? <\/P>\n<P>Well, let\u2019s see if we can figure it out. We start out by declaring an array named arrMondays, and sizing this array so that it can hold two items:<\/P><PRE class=\"codeSample\">Dim arrMondays(1)\n<\/PRE>\n<P>Why two items? Because we need to determine the dates of the first two Mondays in the month. Two Mondays, two slots in the array.<\/P>\n<P>Next we use this block of code to construct a date equal to the first day of the current month:<\/P><PRE class=\"codeSample\">dtmMonth = Month(Date)\ndtmYear = Year(Date)\ndtmDate = CDate(dtmMonth &amp; &#8220;\/1&#8221; &amp; &#8220;\/&#8221; &amp; dtmYear)\n<\/PRE>\n<P>As you can see, in the first two lines of code we use the <B>Month<\/B> function to determine the current month, then use the <B>Year<\/B> function to determine the current year. In line 3, we string those two values, the number <B>1<\/B> and a few \/ marks together in order to create a date like this:<\/P><PRE class=\"codeSample\">2\/1\/2007\n<\/PRE>\n<P>Good question: what <I>is<\/I> the <B>CDate<\/B> function for in the preceding line of code? Well, CDate is short for character-to-date; it\u2019s designed to convert a string value to a date-time value. We include it here to make sure that 2\/1\/2007 gets treated as a date and not as a string.<\/P>\n<P>That\u2019s another good question: why do we care about the first day of the month anyway? Well, we have no idea when the first and second Mondays occur; on top of that, we don\u2019t know any fancy way to determine when the first and second Mondays <I>do<\/I> occur. So we\u2019re going to simply start with the first day of the month and see if it happens to be a Monday. We\u2019ll then check the second day of the month and see if <I>it<\/I> happens to be a Monday. And then we\u2019ll check the third day of the month and \u2026. We\u2019ll continue checking, day-by-day, until we find the first Monday. And once we know the date of the first Monday all we\u2019ll have to do is add 7 days and we\u2019ll know the date of the second Monday.<\/P>\n<TABLE class=\"dataTable\" id=\"ESF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. And, yes, suppose we wanted to run the script on the first and third Mondays: in that case, all we\u2019d need to do is add 14 to the date of the first Monday.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s the block of code we use to determine the dates of the first two Mondays:<\/P><PRE class=\"codeSample\">Do Until i = 1\n    intWeekDay = Weekday(dtmDate)\n    If intWeekDay = 2 Then\n        arrMondays(0) = dtmDate\n        arrMondays(1) = dtmDate + 7\n        Exit Do\n    End If\n    dtmDate = dtmDate + 1\nLoop\n<\/PRE>\n<P>What we\u2019ve done here is set up a Do Until loop designed to run until the counter variable i is equal to 0; in effect this is a loop designed to run until we specifically exit the thing. (Why? Because the way the script is constructed, i will <I>never<\/I> be equal to 0.) Inside that loop we use the <B>Weekday<\/B> function to determine the day of the week for the first day in the month:<\/P><PRE class=\"codeSample\">intWeekDay = Weekday(dtmDate)\n<\/PRE>\n<P>The Weekday function returns an integer value for the day of the week: 1 for Sunday, 2 for Monday, 3 for Tuesday, etc. Because we only care about Mondays we check to see if intWeekDay is equal to 2. Suppose it isn\u2019t. In that case, we simply add one day to the date-time value dtmDate, loop around, and make this same check with the second day of the month:<\/P><PRE class=\"codeSample\">dtmDate = dtmDate + 1\n<\/PRE>\n<P>But what happens when we <I>do<\/I> find a Monday? In that case, we assign the value of dtmDate to the first item in the array; we then add 7 days to that value and assign the resulting date to the second item in the array:<\/P><PRE class=\"codeSample\">arrMondays(0) = dtmDate\narrMondays(1) = dtmDate + 7\n<\/PRE>\n<P>And then we call the <B>Exit Do<\/B> statement and exit the loop.<\/P>\n<P>All that\u2019s left now is to loop through the two dates in the array and check to see if either one happens to be the current date; if it is, then the script must be running on either the first or second Monday of the month. If it <I>is<\/I> the first or second Monday we\u2019d then run a specified bit of code. In our sample script, we simply echo back whether we should (or should not) perform the appointed task:<\/P><PRE class=\"codeSample\">For Each strMonday in arrMondays\n    If strMonday = Date Then\n        Wscript.Echo &#8220;Carry out the task.&#8221;\n    Else\n        Wscript.Echo &#8220;Don&#8217;t carry out the task.&#8221;\n    End If\nNext<\/PRE>\n<P>That\u2019s all there is to it.<\/P>\n<P>You know, speaking of time, time is definitely running out on the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A>; in fact, the Games officially end tomorrow morning (February 23, 2007) at 8:00 Redmond time. We know you have your eyes on one of those <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games07\/bobble.mspx\"><B>Dr. Scripto Bobblehead dolls<\/B><\/A>, and there\u2019s only one way to get one: enter at least one event in the Scripting Games, and thus get your name in the Bobblehead drawing. Did we mention that time is running out? Oh. Well, it\u2019s worth mentioning again: time is running out.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have a logon script that needs to perform certain tasks only on the first and second Mondays of the month. How can I determine whether the script is running on the first or second Monday in a month?&#8212; WA Hey, WA. You know, a few months ago the Scripting Guy who [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,2,3,4,5],"class_list":["post-65453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We have a logon script that needs to perform certain tasks only on the first and second Mondays of the month. How can I determine whether the script is running on the first or second Monday in a month?&#8212; WA Hey, WA. You know, a few months ago the Scripting Guy who [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}