{"id":66603,"date":"2006-08-25T12:08:00","date_gmt":"2006-08-25T12:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/25\/how-can-i-determine-the-first-friday-in-a-month\/"},"modified":"2006-08-25T12:08:00","modified_gmt":"2006-08-25T12:08:00","slug":"how-can-i-determine-the-first-friday-in-a-month","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-first-friday-in-a-month\/","title":{"rendered":"How Can I Determine the First Friday in a Month?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I determine the first Friday in a month?<BR><BR>&#8212; JB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JB. You know, when we first glanced at this question we were hoping it said, \u201cHow can I determine if any given day in a month is a Friday?\u201d Why? Well, if that was the case then we had a sure-fire method for you: just walk through the cafeteria in the Scripting Guys building at lunch time. If you walk into the cafeteria at noon on, say, a Monday or a Thursday, the place will be jam-packed; walk through on a Friday, however, and you feel like you\u2019re the last person on Earth. <I>Nobody<\/I> is around on a Friday.<\/P>\n<TABLE id=\"E6C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Is that because all these dedicated Microsoft employees are working through lunch, making sure they get everything done before the weekend? Um, sure; why not?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, that wasn\u2019t your question; instead, you wanted to know how you can identify the first Friday in a month. Admittedly, you might not find the answer to that simply by walking through the Microsoft cafeteria. Instead, you might need to use a script similar to this:<\/P><PRE class=\"codeSample\">dtmDate = #11\/1\/2006#<\/p>\n<p>Do Until x = 1\n    intDayOfWeek = Weekday(dtmDate)\n    If intDayOfWeek = 6 Then\n        Wscript.Echo &#8220;The first Friday of the month is &#8221; &amp; dtmDate &amp; &#8220;.&#8221;\n        Exit Do\n    Else\n        dtmDate = dtmDate + 1\n    End If\nLoop\n<\/PRE>\n<P>What\u2019s that? Sure, we can spare a few minutes to explain how this script works. After all, it\u2019s Friday; we pretty much have the place to ourselves. <\/P>\n<P>As you can see, we start out by assigning a date \u2013 November 1, 2006 \u2013 to a variable named dtmDate:<\/P><PRE class=\"codeSample\">dtmDate = #11\/1\/2006#\n<\/PRE>\n<TABLE id=\"EPD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Are the pound signs \u2013 the # characters \u2013 required when assigning a date to a variable? Not necessarily, but it\u2019s a good idea. When VBScript sees the pound signs there\u2019s no confusion; it <I>knows<\/I> that the value being assigned is supposed to be a date.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Oh, right: there\u2019s a very good reason why we set the date to November <I>first<\/I>. After all, we want to determine the first Friday in the month; therefore we need to start with day 1, just in case the first day in November <I>is<\/I> a Friday. If it is, great. If it isn\u2019t, then we\u2019ll check to see if the <I>second<\/I> day in the month is a Friday. And so on. At least that\u2019s the plan; let\u2019s see if it works.<\/P>\n<P>After assigning the date we then set up a Do Until loop that runs until a variable named x is equal to 1. Because x has never been assigned a value, its current value is 0; in fact, the value of x will <I>always<\/I> be a zero. In other words, we\u2019ve set up a loop designed to run forever. But don\u2019t panic: after all, in the scripting world forever doesn\u2019t always take very long.<\/P>\n<TABLE id=\"ENE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. What does <I>that<\/I> mean? Good question; hopefully we\u2019ll figure it out ourselves before we reach the end of this column.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Inside the loop the very first thing we do is use the <B>Weekday<\/B> function to determine the integer value corresponding to the given date:<\/P><PRE class=\"codeSample\">intDayOfWeek = Weekday(dtmDate)\n<\/PRE>\n<P>Depending on the day of the week, the Weekday function will return one of the following values:<\/P>\n<TABLE id=\"ECF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Day of the Week<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Sunday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Monday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Tuesday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Wednesday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Thursday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Friday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">Saturday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">7<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Because we\u2019re interested in only Fridays we use this line of code to determine whether or not the Weekday value for 11\/1\/2006 (the current value of dtmDate) is equal to 6:<\/P><PRE class=\"codeSample\">If intDayOfWeek = 6 Then\n<\/PRE>\n<P>Suppose the Weekday value for 11\/1\/2006 <I>is<\/I> 6 (it isn\u2019t, but suppose it is). In that case we echo back the fact that the first Friday in November, 2006 is 11\/1\/2006 and then call the <B>Exit Do<\/B> statement to exit our not-so-endless loop:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;The first Friday of the month is &#8221; &amp; dtmDate &amp; &#8220;.&#8221;\nExit Do\n<\/PRE>\n<TABLE id=\"EAH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, even though our loop is designed to run forever all we have to do is call Exit Do and we\u2019ll immediately exit the thing. If only we had a similar function to help us exit those meetings that also seem designed to run forever.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That\u2019s nice, but what if the Weekday value for 11\/1\/2006 <I>isn\u2019t<\/I> equal to 6 (which it isn\u2019t)? No problem; in that case we simply add 1 day to the date (making the value of dtmDate equal to 11\/2\/2006) and then loop around and use the Weekday function to test <I>that<\/I> date:<\/P><PRE class=\"codeSample\">dtmDate = dtmDate + 1\n<\/PRE>\n<P>This process continues until we finally encounter a Friday. When that happens, we\u2019ll get back a message similar to this, we\u2019ll exit the loop, and the script will terminate:<\/P><PRE class=\"codeSample\">The first Friday of the month is 11\/3\/2006.\n<\/PRE>\n<P>Nice.<\/P>\n<P>By the way, if you want to get a head start on next year, here\u2019s a script (we\u2019ll leave it up to you to figure out exactly how it works) that reports back the first Friday in every month for the year 2007:<\/P><PRE class=\"codeSample\">For i = 1 to 12\n    dtmDate = CDate(i &amp; &#8220;\/1\/2007&#8221;)<\/p>\n<p>    Do Until x = 1\n        intDayOfWeek = Weekday(dtmDate)\n        If intDayOfWeek = 6 Then\n            Wscript.Echo &#8220;The first Friday of the month is &#8221; &amp; dtmDate &amp; &#8220;.&#8221;\n            Exit Do\n        Else\n            dtmDate = dtmDate + 1\n        End If\n    Loop\nNext\n<\/PRE>\n<P>That\u2019s going to give us back data that looks like this:<\/P><PRE class=\"codeSample\">The first Friday of the month is 1\/5\/2007.\nThe first Friday of the month is 2\/2\/2007.\nThe first Friday of the month is 3\/2\/2007.\nThe first Friday of the month is 4\/6\/2007.\nThe first Friday of the month is 5\/4\/2007.\nThe first Friday of the month is 6\/1\/2007.\nThe first Friday of the month is 7\/6\/2007.\nThe first Friday of the month is 8\/3\/2007.\nThe first Friday of the month is 9\/7\/2007.\nThe first Friday of the month is 10\/5\/2007.\nThe first Friday of the month is 11\/2\/2007.\nThe first Friday of the month is 12\/7\/2007.\n<\/PRE>\n<P>The secret here is that we\u2019ve added a second loop. We want to report data back for 12 months, so we set up a For Next loop that runs from 1 through 12. Then, we use the counter variable for that loop (which we named i) in a little formula that determines the first day in each month:<\/P><PRE class=\"codeSample\">dtmDate = CDate(i &amp; &#8220;\/1\/2007&#8221;)\n<\/PRE>\n<P>As you can see, when i is equal to 1 we\u2019ll be assigning dtmDate the value <B>1<\/B>\/1\/2007; when i is equal to 2 (which it will be the second time through the loop) then we\u2019ll be assigning dtmDate the value <B>2<\/B>\/1\/2007. Etc.<\/P>\n<TABLE id=\"EGAAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. The function <B>CDate<\/B>? CDate is short for <I>character-to-date<\/I>, and is designed to transform a string value into a date-time value. Again, we\u2019re just making sure that VBScript treats dtmDate as a date. And sure, we could have used CDate (instead of pound signs) when assigning the date in our first script. Why didn\u2019t we? No reason, really; we just felt like using pound signs.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>One last thing. If the Scripting Editor tries to slip a note in here saying that this column exaggerates the number of people who skip \u2026 lunch \u2026 on a Friday, well, look at it this way. Who are you going to trust when it comes to being lazy and weaseling out work: the Scripting Editor, or the Scripting Guy who writes this column? (Or, to put it a little more accurately, the Scripting Guy who writes this column if he doesn\u2019t have to coach baseball or if he isn\u2019t taking a week off or \u2026.) <\/P>\n<P>That\u2019s what we thought.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the first Friday in a month?&#8212; JB Hey, JB. You know, when we first glanced at this question we were hoping it said, \u201cHow can I determine if any given day in a month is a Friday?\u201d Why? Well, if that was the case then we had a [&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,3,4,5],"class_list":["post-66603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine the first Friday in a month?&#8212; JB Hey, JB. You know, when we first glanced at this question we were hoping it said, \u201cHow can I determine if any given day in a month is a Friday?\u201d Why? Well, if that was the case then we had a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66603","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=66603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66603\/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=66603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}