How Can I Determine the Beginning and Ending Date of the Previous Month?

ScriptingGuy1

Hey, Scripting Guy! Question

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.

— PH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PH. You know, this was a tough question for us: after all, as Microsoft employees we’re always looking forward; we never look back. Last month? We don’t even know the meaning of the words.

OK, true: the Scripting Guys don’t know the meaning of lots of words. But you know what we mean.

But guess what? If a reader of the Hey, Scripting Guy! column needs a script that can tell you the beginning and ending dates of the previous month, well, by golly that’s what that reader’s going to get:

dtmTargetDate = Date
intDay = Day(dtmTargetDate)

dtmLastDay = dtmTargetDate – intDay Wscript.Echo “Last day of previous month: ” & dtmLastDay

dtmFirstDay = Month(dtmLastDay) & “/1/” & Year(dtmLastDay) Wscript.Echo “First day of previous month: ” & dtmFirstDay

Yes, just a few cryptic-looking lines (don’t ask us any questions about DNS or DHCP, because we seem to have used up our entire quota of the letter D for the month). It’s actually a fairly simple little script.

The first line of code simply specifies the target date. In this sample script we’re using the current date (indicated by the Date function). However, we can specify any date we want in this first line. For example:

dtmTargetDay = #12/19/1989#

In line 2 we use the VBScript Day 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 is 8/3/2005 and – using our date arithmetic skills – we subtract 3 days from that date. That doesn’t 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:

dtmLastDay = dtmTargetDate – intDay
Wscript.Echo “Last day of previous month: ” & dtmLastDay

Calculating the first day of the month is even easier, in part because we cheat a little. It’s 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:

dtmFirstDay = Month(dtmLastDay) & “/1/” & Year(dtmLastDay)
Wscript.Echo “First day of previous month: ” & dtmFirstDay

When we run the script using 8/3/2005 as the target date we get this output:

Last day of previous month: 7/31/2005
First day of previous month: 7/1/2005

There you have it: a script that will actually transport you back in time. Could Back to the Future: The Scripting Edition be far behind?

0 comments

Discussion is closed.

Feedback usabilla icon