{"id":66923,"date":"2006-07-12T11:22:00","date_gmt":"2006-07-12T11:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/12\/how-can-i-determine-last-months-date\/"},"modified":"2006-07-12T11:22:00","modified_gmt":"2006-07-12T11:22:00","slug":"how-can-i-determine-last-months-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-last-months-date\/","title":{"rendered":"How Can I Determine Last Month\u2019s Date?"},"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 last month\u2019s date? I need to be able to take the month and year for the previous month (using the format <I>mmyy<\/I>) and create a file name like this: test0606.xls.<BR><BR>&#8212; DR<\/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=\"TechNet Script Center\" border=\"0\" alt=\"TechNet 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, DR. We have to admit that, at first, this question posed a bit of a problem for the Scripting Guys. Not because this is an especially tough script to write; it isn\u2019t. It\u2019s just that we never have to bother with scripts to find out about things that happened in the past: if we want old and out-of-date information we just go ask Greg. If you ever need information about something no else cares about anymore, trust us: Greg\u2019s the guy to see.<\/P>\n<P>On the other hand, Greg always seems to leave early to coach baseball, and he adamantly refuses to make house calls; that means there\u2019s little chance he\u2019ll drop by and help you determine last month\u2019s date. (Granted, no one has ever actually <I>asked<\/I> him to make a house call, but he still refuses out of general principle.) So if Greg won\u2019t help us are we out of luck here? Of course not; as it turns out, Greg can be more-than-adequately replaced by just a few lines of VBScript code:<\/P><PRE class=\"codeSample\">intMonth = Month(Date)\nintYear = Year(Date)<\/p>\n<p>If intMonth = 1 Then\n    intMonth = 12\n    intYear = intYear &#8211; 1\nElse\n    intMonth = intMonth &#8211; 1\nEnd If<\/p>\n<p>If intMonth &lt; 10 Then\n    intMonth = &#8220;0&#8221; &amp; intMonth\nEnd If<\/p>\n<p>intYear = Right(intYear, 2)<\/p>\n<p>strFileName = &#8220;test&#8221; &amp; intMonth &amp; intYear &amp; &#8220;.xls&#8221;<\/p>\n<p>Wscript.Echo strFileName\n<\/PRE>\n<P>As you can see, determining the current month and the current year is pretty easy: we just need to use the <B>Month<\/B> and <B>Year<\/B> functions, respectively:<\/P><PRE class=\"codeSample\">intMonth = Month(Date)\nintYear = Year(Date)\n<\/PRE>\n<P>Those two lines of code are going to give us back values like <B>7<\/B> and <B>2006<\/B>, assuming that we\u2019re currently in July, 2006. And, as usual, you\u2019re way ahead of us: if the current month is equal to 7, then all we have to do is subtract 1 from the current month and then we\u2019ll know the previous month: month number 6 (June). Case closed!<\/P>\n<P>Or is it? As Greg would be the first to point out, there\u2019s a problem with that approach. Suppose we run our script on January 3, 2006. January is month 1; if we subtract 1 from 1 we get \u2026 um, month 0. On top of that, the month prior to January 2006 is December <I>2005<\/I>, but we still have 2006 as the year. Uh-oh.<\/P>\n<P>Wait, put down the phone; there\u2019s no need to call Greg. Here\u2019s how we can overcome that little obstacle:<\/P><PRE class=\"codeSample\">If intMonth = 1 Then\n    intMonth = 12\n    intYear = intYear &#8211; 1\nElse\n    intMonth = intMonth &#8211; 1\nEnd If\n<\/PRE>\n<P>All we\u2019re doing here is setting up an If-Then statement to check the value of intMonth. If intMonth is equal to 1 (which will be the case if we run the script in January) we use these two lines of code to set the month to 12 (December) and the year to the current year minus 1 (e.g., 2006 &#8211; 1, or 2005):<\/P><PRE class=\"codeSample\">intMonth = 12\nintYear = intYear &#8211; 1\n<\/PRE>\n<P>If intMonth is equal to anything other than 1 then we simply assign the variable a new value: the current value (e.g., 7) minus 1. We\u2019re now able to correctly determine the previous month and year, even if we happen to run the script in January.<\/P>\n<P>Oh: right. Greg (who\u2019s now feeling a bit miffed because we don\u2019t need his help) has just pointed out another problem. DR wants to use the format <I>mmyy<\/I>; that is, two digits for the month and two digits for the year. In other words, he wants 06 and 06, and we currently have 6 and 2006. <I>Now<\/I> is it time to go crawling back to Greg, begging for his help?<\/P>\n<P>Listen, we don\u2019t need Greg\u2019s help in order to turn 6 2006 into 06 06. For starters, let\u2019s take a look at a block of code that will turn 6 into 06:<\/P><PRE class=\"codeSample\">If intMonth &lt; 10 Then\n    intMonth = &#8220;0&#8221; &amp; intMonth\nEnd If\n<\/PRE>\n<P>Nothing to it, right? Here we\u2019re simply checking to see if the value of intMonth is less than 10. Why 10? Well, if intMonth is equal to 10, 11, or 12, then there\u2019s no problem; in that case the month already <I>has<\/I> two digits. We\u2019re only concerned about months 1 through 9, the single-digit months. If we\u2019re dealing with one of those months we just use this line of code to place a leading zero before the month number:<\/P><PRE class=\"codeSample\">intMonth = &#8220;0&#8221; &amp; intMonth\n<\/PRE>\n<P>Just like that intMonth changes from 6 to 06. Which is just what we wanted it to do.<\/P>\n<P>Converting 2006 to 06 is even easier; to handle that problem we just use the <B>Right<\/B> function to grab the two right-most characters off the end of 2006:<\/P><PRE class=\"codeSample\">intYear = Right(intYear, 2)\n<\/PRE>\n<P>Once that line of code executes the variable intMonth will be equal to 06 and the variable intYear will be equal to 06. That means we can then use this line of code to piece together our file name:<\/P><PRE class=\"codeSample\">strFileName = &#8220;test&#8221; &amp; intMonth &amp; intYear &amp; &#8220;.xls&#8221;\n<\/PRE>\n<P>And that\u2019s about it. To verify that the process worked we finish the script by echoing back the new file name:<\/P><PRE class=\"codeSample\">test0606.xls\n<\/PRE>\n<P>Cool. And we didn\u2019t need Greg\u2019s help at all. <\/P>\n<TABLE id=\"EYE\" 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>. In all fairness, we should point out that Greg still has <I>some<\/I> use around here. After all, if we ever need to know why they called the infield fly rule even though an outfielder made the catch, or if we absolutely <I>have<\/I> to know what happens if a fielder tosses his glove at the ball, well, Greg will still be the guy we turn to. Of course, other than that \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine last month\u2019s date? I need to be able to take the month and year for the previous month (using the format mmyy) and create a file name like this: test0606.xls.&#8212; DR Hey, DR. We have to admit that, at first, this question posed a bit of a problem [&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-66923","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 last month\u2019s date? I need to be able to take the month and year for the previous month (using the format mmyy) and create a file name like this: test0606.xls.&#8212; DR Hey, DR. We have to admit that, at first, this question posed a bit of a problem [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66923","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=66923"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66923\/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=66923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}