{"id":69873,"date":"2005-05-05T07:47:00","date_gmt":"2005-05-05T07:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/05\/how-can-i-determine-the-week-of-the-month-a-date-falls-in\/"},"modified":"2005-05-05T07:47:00","modified_gmt":"2005-05-05T07:47:00","slug":"how-can-i-determine-the-week-of-the-month-a-date-falls-in","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-week-of-the-month-a-date-falls-in\/","title":{"rendered":"How Can I Determine the Week of the Month a Date Falls In?"},"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 week of the month a date falls in?<BR><BR>&#8212; AK<\/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, AK. You\u2019re probably familiar with the book <I>Moby Dick<\/I>, the story of a crazy sea captain who became obsessed with hunting down and finishing off the great white whale. Well, this question turned out to be the Moby Dick of the scripting world. When we first read it we turned to the VBScript documentation, assuming we\u2019d find the WeekOfTheMonth function in there. We didn\u2019t: no such function exists. We then did a cursory search of the newsgroups and couldn\u2019t find an answer there either. And then we became absolutely <I>obsessed<\/I> with figuring out how you can determine the week of the month a date falls in.<\/P>\n<P>Well, sort of obsessed: we didn\u2019t actually do anything about it, although every now and then we\u2019d think, \u201cMan, we should try to figure out that week of the month thing.\u201d And then finally, a couple days ago, we sat down and tried to come up with a solution.<\/P>\n<P>And guess what: we came up with one. We\u2019re not claiming this is a particularly elegant solution, and we have no doubt that there are better and more efficient ways of solving this problem. But hey, it works, and that\u2019s really the bottom-line, right? (As you might expect, it\u2019s always us less-than-elegant types who argue that elegance doesn\u2019t really matter.)<\/P>\n<P>Here\u2019s a script that will tell you the week of the month that December 19, 2005 falls in:<\/P><PRE class=\"codeSample\">dtmTargetDate = #12\/19\/2005#   <\/p>\n<p>dtmDay = DatePart(&#8220;d&#8221;, dtmTargetDate)\ndtmMonth = DatePart(&#8220;m&#8221;, dtmTargetDate)\ndtmYear = DatePart(&#8220;yyyy&#8221;, dtmTargetDate)<\/p>\n<p>dtmStartDate = dtmMonth &amp; &#8220;\/1\/&#8221; &amp; dtmYear\ndtmStartDate = CDate(dtmStartDate)<\/p>\n<p>intWeekday = Weekday(dtmStartDate)\nintAddon = 8 &#8211; intWeekday<\/p>\n<p>intWeek1 = intAddOn\nintWeek2 = intWeek1 + 7\nintWeek3 = intWeek2 + 7\nintWeek4 = intWeek3 + 7\nintWeek5 = intWeek4 + 7\nintWeek6 = intWeek5 + 7<\/p>\n<p>If dtmDay &lt;= intWeek6 Then\n    strWeek = &#8220;Week 6&#8221;\nEnd If<\/p>\n<p>If dtmDay &lt;= intWeek5 Then\n    strWeek = &#8220;Week 5&#8221;\nEnd If<\/p>\n<p>If dtmDay &lt;= intWeek4 Then\n    strWeek = &#8220;Week 4&#8221;\nEnd If<\/p>\n<p>If dtmDay &lt;= intWeek3 Then\n    strWeek = &#8220;Week 3&#8221;\nEnd If<\/p>\n<p>If dtmDay &lt;= intWeek2 Then\n    strWeek = &#8220;Week 2&#8221;\nEnd If<\/p>\n<p>If dtmDay &lt;= intWeek1 Then\n    strWeek = &#8220;Week 1&#8221;\nEnd If<\/p>\n<p>Wscript.Echo strWeek\n<\/PRE>\n<P>This is a semi-complicated script so we only have room in this column to provide an overview of how it works; if you want the gory details you\u2019ll have to sort them out for yourself. We should also note that this script assumes that the first week in the month is whatever week day 1 falls in; we\u2019re not interested in the first full week of the month or the first week with a workday in it or anything like that. If you want to calculate weeks like that, well, you\u2019re on your own there, too.<\/P>\n<P>But enough about that. The script itself is built on the assumption that the key to determining the week of the month that a date falls in is this: determining the day of the week for the first day of the month. Why? Well, suppose day 1 falls on a Saturday. That means day 2 falls on a Sunday which &#8211; for our purposes &#8211; would mean that day 2 occurs in week 2. But suppose day 1 falls on a Friday? In that case, day 2 falls on a Saturday which &#8211; again, for our purposes &#8211; would mean that day 2 falls in week 1.<\/P>\n<P>Confused? Take a look at the calendar for December 2005. We\u2019re saying that December 1, 2, and 3 fall in week 1; December 4 marks the first day of week 2:<\/P><IMG border=\"0\" alt=\"December 2005\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/calendar.jpg\" width=\"335\" height=\"289\"> \n<P><BR>From this picture we know that our date &#8211; December 19, 2005 &#8211; falls in week 4. But how do we determine that programmatically?<\/P>\n<P>We begin by using VBScript\u2019s <B>DatePart<\/B> function to extract the day (<B>d<\/B>), month (<B>m<\/B>), and year (<B>yyyy<\/B>) from the date:<\/P><PRE class=\"codeSample\">dtmDay = DatePart(&#8220;d&#8221;, dtmTargetDate)\ndtmMonth = DatePart(&#8220;m&#8221;, dtmTargetDate)\ndtmYear = DatePart(&#8220;yyyy&#8221;, dtmTargetDate)\n<\/PRE>\n<P>We then construct a new date representing December 1, 2005 using this code:<\/P><PRE class=\"codeSample\">dtmStartDate = dtmMonth &amp; &#8220;\/1\/&#8221; &amp; dtmYear\ndtmStartDate = CDate(dtmStartDate)\n<\/PRE>\n<P>In the first line we put together the date string &#8211; 12\/1\/2005 &#8211; and in the second line we use the <B>CDate<\/B> function to ensure that VBScript treats the string as a date-time value. That\u2019s not too bad, is it?<\/P>\n<P>But now it gets a little crazy. We begin by using the <B>Weekday<\/B> function to determine the day of the week for December 1<SUP>st<\/SUP>:<\/P><PRE class=\"codeSample\">intWeekday = Weekday(dtmStartDate)\n<\/PRE>\n<P>Weekday returns an integer value ranging from 1 (Sunday) to 7 (Saturday). Because December 1<SUP>st<\/SUP> falls on a Thursday we get back a 5.<\/P>\n<P>Next we need to determine which date represents the last day in week 1; that\u2019s important because once we know that we can calculate the end dates for each week. And once we know <I>that<\/I> we can figure out which week any given date falls in. To determine the end date for week 1 we use this code:<\/P><PRE class=\"codeSample\">intAddon = 8 &#8211; intWeekday\n<\/PRE>\n<P><I>Why<\/I> do we use that code? Good question. If you look at the calendar, December 1 occurs on a Thursday, which has an integer value of 5. If we subtract 5 from 8 we get 3. And take another look at the calendar: December 3<SUP>rd<\/SUP> just happens to be the last day of week 1. Turns out that all we have to do is subtract the Weekday value from 8 and we\u2019ll know the date for the last day of week 1.<\/P>\n<P>Yeah, we know. But play around with it a bit and you\u2019ll see that it works.<\/P>\n<P>Now that we know that week 1 ends on December 3<SUP>rd<\/SUP> we can easily calculate the end dates for every other week; after all, week 2 will end on December 3<SUP>rd<\/SUP> plus 7 days, or December 10<SUP>th<\/SUP>. Because a month can have as many as six weeks we go ahead and calculate end dates for six weeks (the fact that most months <I>won\u2019t<\/I> have six weeks doesn\u2019t matter):<\/P><PRE class=\"codeSample\">intWeek1 = intAddOn\nintWeek2 = intWeek1 + 7\nintWeek3 = intWeek2 + 7\nintWeek4 = intWeek3 + 7\nintWeek5 = intWeek4 + 7\nintWeek6 = intWeek5 + 7\n<\/PRE>\n<P>This next part we could have done in a few less lines of code, but we wanted to make it clear what we\u2019re doing; therefore we put the code together using a bunch of If-Then statements. We\u2019ve already determined the day part of our target date: 19. And now we\u2019ve determined the end date for each week in the month:<\/P>\n<TABLE id=\"ERF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Week<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>End Date<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">38<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">31<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">23<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">17<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">10<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Our next step is to see if our day &#8211; 19 &#8211; is less than or equal to the end date for the various weeks. Is 19 less than the week 6 end date of 38? Yes, so we set the value of the variable strWeek to \u201cWeek 6\u201d. Is it less than the week 5 end date of 31? Yes, so we now set the value of strWeek to \u201cWeek 5\u201d. How about week 4? Yes, so now strWeek equals \u201cWeek 4\u201d. Make sense? <\/P>\n<P>Ok, so what about week 3? Is 19 less than or equal to 17? No, so we don\u2019t change the value of strWeek; we leave it alone. When we finish the last of our If-Then statements we echo the results: turns out that December 19, 2005 falls in week 4 of the month.<\/P>\n<P>Again, a cursory explanation for a semi-complicated script and we apologize for that. But at least the script works. And as crazy as the script might seem, it was still a heck of a lot easier than trying to hunt down and kill a great white whale (even when it comes to our obsessions we try to take the easiest possible route).<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the week of the month a date falls in?&#8212; AK Hey, AK. You\u2019re probably familiar with the book Moby Dick, the story of a crazy sea captain who became obsessed with hunting down and finishing off the great white whale. Well, this question turned out to be the [&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-69873","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 week of the month a date falls in?&#8212; AK Hey, AK. You\u2019re probably familiar with the book Moby Dick, the story of a crazy sea captain who became obsessed with hunting down and finishing off the great white whale. Well, this question turned out to be the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69873","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=69873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69873\/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=69873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}