{"id":69193,"date":"2005-08-11T07:08:00","date_gmt":"2005-08-11T07:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/11\/how-can-i-determine-the-beginning-and-ending-date-of-the-previous-month\/"},"modified":"2005-08-11T07:08:00","modified_gmt":"2005-08-11T07:08:00","slug":"how-can-i-determine-the-beginning-and-ending-date-of-the-previous-month","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-beginning-and-ending-date-of-the-previous-month\/","title":{"rendered":"How Can I Determine the Beginning and Ending Date of the Previous 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! Given a specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8\/11\/2005, I need to get back 7\/1\/2005 and 7\/31\/2005.<BR><BR>&#8212; PH<\/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, PH. You know, this was a tough question for us: after all, as Microsoft employees we\u2019re always looking forward; we never look back. Last month? We don\u2019t even know the meaning of the words.<\/P>\n<P>OK, true: the Scripting Guys don\u2019t know the meaning of <I>lots<\/I> of words. But you know what we mean.<\/P>\n<P>But guess what? If a reader of the <I>Hey, Scripting Guy!<\/I> column needs a script that can tell you the beginning and ending dates of the previous month, well, by golly that\u2019s what that reader\u2019s going to get:<\/P><PRE class=\"codeSample\">dtmTargetDate = Date\nintDay = Day(dtmTargetDate)<\/p>\n<p>dtmLastDay = dtmTargetDate &#8211; intDay\nWscript.Echo &#8220;Last day of previous month: &#8221; &amp; dtmLastDay<\/p>\n<p>dtmFirstDay = Month(dtmLastDay) &amp; &#8220;\/1\/&#8221; &amp; Year(dtmLastDay)\nWscript.Echo &#8220;First day of previous month: &#8221; &amp; dtmFirstDay\n<\/PRE>\n<P>Yes, just a few cryptic-looking lines (don\u2019t ask us any questions about DNS or DHCP, because we seem to have used up our entire quota of the letter <I>D<\/I> for the month). It\u2019s actually a fairly simple little script.<\/P>\n<P>The first line of code simply specifies the target date. In this sample script we\u2019re using the current date (indicated by the <B>Date<\/B> function). However, we can specify any date we want in this first line. For example:<\/P><PRE class=\"codeSample\">dtmTargetDay = #12\/19\/1989#\n<\/PRE>\n<P>In line 2 we use the VBScript <B>Day<\/B> function to extract the day portion of the current date; if the date happens to be 8\/3\/2005, then intDay will be equal to 3. Why do we care about the day portion of the date? Well, suppose it <I>is<\/I> 8\/3\/2005 and &#8211; using our date arithmetic skills &#8211; we subtract 3 days from that date. That doesn\u2019t give us 8\/0\/2005; instead, it gives us 7\/31\/2005, which just happens to be the last day of the previous month. As it turns out, determining the last day of the previous month is no more difficult than subtracting the day (3) from the date and then echoing the resulting value:<\/P><PRE class=\"codeSample\">dtmLastDay = dtmTargetDate &#8211; intDay\nWscript.Echo &#8220;Last day of previous month: &#8221; &amp; dtmLastDay\n<\/PRE>\n<P>Calculating the first day of the month is even easier, in part because we cheat a little. It\u2019s pretty much a guarantee that the first day of the month will always be day 1. Therefore, all we need to do is grab the month and year from our variable dtmLastDay (which holds the value of the last day of the previous month), jam everything together, and then echo the result:<\/P><PRE class=\"codeSample\">dtmFirstDay = Month(dtmLastDay) &amp; &#8220;\/1\/&#8221; &amp; Year(dtmLastDay)\nWscript.Echo &#8220;First day of previous month: &#8221; &amp; dtmFirstDay\n<\/PRE>\n<P>When we run the script using 8\/3\/2005 as the target date we get this output:<\/P><PRE class=\"codeSample\">Last day of previous month: 7\/31\/2005\nFirst day of previous month: 7\/1\/2005\n<\/PRE>\n<P>There you have it: a script that will actually transport you back in time. Could <I>Back to the Future: The Scripting Edition<\/I> be far behind?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Given a specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8\/11\/2005, I need to get back 7\/1\/2005 and 7\/31\/2005.&#8212; PH Hey, PH. You know, this was a tough question for us: after all, as Microsoft employees we\u2019re always looking forward; 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-69193","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 specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8\/11\/2005, I need to get back 7\/1\/2005 and 7\/31\/2005.&#8212; PH Hey, PH. You know, this was a tough question for us: after all, as Microsoft employees we\u2019re always looking forward; we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69193","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=69193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69193\/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=69193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}