{"id":69533,"date":"2005-06-23T14:50:00","date_gmt":"2005-06-23T14:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/23\/how-can-i-determine-the-date-for-the-friday-in-a-given-week\/"},"modified":"2005-06-23T14:50:00","modified_gmt":"2005-06-23T14:50:00","slug":"how-can-i-determine-the-date-for-the-friday-in-a-given-week","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-date-for-the-friday-in-a-given-week\/","title":{"rendered":"How Can I Determine the Date for the Friday in a Given Week?"},"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! Given a date, can a script tell me the date that the Friday for that week occurs?<BR><BR>&#8212; DLR<\/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, DLR. Too bad you didn\u2019t ask which dates Saturdays, Sundays, and holidays fall on; coincidentally enough, each of the Scripting Guys have memorized &#8211; years in advance &#8211; all the days that we aren\u2019t required to come to work. When it comes to work days like Fridays, however, we don\u2019t do quite as well.<\/P>\n<P>But that\u2019s OK: we don\u2019t need to know the dates of all the Fridays because we can get a script to figure that out for us. In fact, here\u2019s a script that &#8211; given a date &#8211; will report back the date that the Friday of that week falls on:<\/P><PRE class=\"codeSample\">dtmDate = #6\/23\/2005#<\/p>\n<p>intDay = WeekDay(dtmDate)\nintAdder = 6 &#8211; intDay\ndtmFriday = dtmDate + intAdder<\/p>\n<p>Wscript.Echo dtmFriday\n<\/PRE>\n<P>How can such a little script accomplish such an amazing feat? Well, let\u2019s see if we can figure that out. The script begins by assigning the date 6\/23\/2005 (June 23, 2005) to a variable named dtmDate. Note that the pound signs (#) are optional; we could have surrounded the date with double quote marks instead. However, using pound signs ensures that VBScript interprets the value as a date and not as something else.<\/P>\n<P>Next we use the <B>WeekDay<\/B> function to determine the day of the week for June 23, 2005 (it\u2019s a Thursday, by the way). WeekDay is going to return one of the following values:<\/P>\n<TABLE id=\"EAD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbSunday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Sunday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbMonday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Monday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbTuesday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Tuesday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbWednesday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Wednesday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbThursday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Thursday<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbFriday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Friday<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbSaturday<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">7<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Saturday<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P><B>Note<\/B>. The above table &#8211; and the script we just showed you &#8211; both assume that Sunday is the first day of the week. <\/P>\n<P>This gives us a value representing the day of the week; in this case we\u2019ll get back a 5, indicating that June 23, 2005 falls on a Thursday. What we need to do now is programmatically determine the number of days between this date and the Friday of that week. That\u2019s something we can do with a single line of code:<\/P><PRE class=\"codeSample\">intAdder = 6 &#8211; intDay\n<\/PRE>\n<P>Confused? Don\u2019t be; this actually makes sense. If you refer to the table you\u2019ll see that Friday has a value of 6. If we take the value for Friday (6) and subtract the value for Thursday (5) we get 1. And guess what? If we add 1 day to June 23, 2005 we\u2019ll get the date of the Friday for that week: June 24, 2005. That\u2019s what we do with this line of code:<\/P><PRE class=\"codeSample\">dtmFriday = dtmDate + intAdder\n<\/PRE>\n<P>See how that works? In fact, this simple little equation even works with Saturdays. Suppose we\u2019re checking the date June 25, 2005, which happens to be a Saturday (and has a WeekDay value of 7). Our equation would work out to this:<\/P><PRE class=\"codeSample\">intAdder = 6 &#8211; 7\n<\/PRE>\n<P>As you well know, 6 &#8211; 7 equals -1. Subtract 1 day from June 25, 2005 and you get June 24, 2005, a Friday. Pretty slick, huh?<\/P>\n<P>Incidentally, there are all sorts of cool things you can do with dates and date arithmetic; for more information, take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_vbs_xvyf.mspx\" target=\"_blank\"><B>VBScript Primer<\/B><\/A> chapter in the Microsoft Windows 2000 Scripting Guide.<\/P>\n<P>As for the Scripting Guys, it\u2019s nice to have a script that can tell you the date that a Friday falls on or the date that a Saturday or Sunday falls on. But we\u2019re not satisfied with that; we plan on writing a script that will turn any given day <I>into<\/I> a Saturday (just a coincidence that we all get Saturdays off.) If you suddenly stop seeing <I>Hey, Scripting Guy!<\/I> columns (which aren\u2019t published on Saturdays) you\u2019ll know we succeeded.<\/P>\n<P>Based on the way our initial research is going, however, you can expect to see a new column tomorrow, same as always. (This is turning out to be much harder than we\u2019d hoped.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Given a date, can a script tell me the date that the Friday for that week occurs?&#8212; DLR Hey, DLR. Too bad you didn\u2019t ask which dates Saturdays, Sundays, and holidays fall on; coincidentally enough, each of the Scripting Guys have memorized &#8211; years in advance &#8211; all the days that we [&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-69533","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! Given a date, can a script tell me the date that the Friday for that week occurs?&#8212; DLR Hey, DLR. Too bad you didn\u2019t ask which dates Saturdays, Sundays, and holidays fall on; coincidentally enough, each of the Scripting Guys have memorized &#8211; years in advance &#8211; all the days that we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69533","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=69533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69533\/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=69533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}