
VBScript solution to Event 5 in the 2008 Winter Scripting Games.
Solutions are also available for Windows PowerShell and Perl.
In this event you needed to learn about some of the date functions available in VBScript, and do a little creative thinking in the process. You also needed to read information entered at the command-line by the user as a parameter to the script call. Here’s the solution we came up with:
dtToday = Now
dtDate = Wscript.Arguments.Item(0)
If dtToday > dtDate Then
Wscript.Echo "Date entered is earlier than today"
Else
Wscript.Echo "Days: " & DateDiff("d", dtToday, dtDate)
iMonth = DateDiff("m", dtToday, dtDate)
Wscript.Echo "Months: " & iMonth
If Day(dtDate) < Day(dtToday) Then
iMonth = iMonth - 1
End If
dtTemp = DateAdd("m", iMonth, dtToday)
Wscript.Echo "Month/Day: " & (iMonth) & " / " & DateDiff("d", dtTemp, dtDate)
End If
Right at the beginning of this script we call one of the date functions: the Now function:
dtToday = Now
This function returns the current date and time from the system clock, which we store in the variable dtToday.
When this script is run, it must be passed a date as the first parameter of the script call, like this:
C:\Scripts> cscript event5.vbs "May 18, 2009"
We need to read that date into our script so we can do our date comparisons. We read the date in by using the Wscript.Arguments.Item collection:
dtDate = Wscript.Arguments.Item(0)
We’ve passed 0 in as the index to the collection, meaning we want the first parameter (index number 0) that is specified at the command line. This will be our date, which we the save to the dtDate variable.
According to the event instructions, you need to compare only dates that are later than the current date. In this script the first thing we do is make sure the date entered really is later than today:
If dtToday > dtDate Then
If today is later than the date entered, we echo an error message and end the script:
Wscript.Echo "Date entered is earlier than today"
No, you didn’t need to do this in order to receive all the points for this event. We promised we’d enter only valid dates, so if you didn’t check for this that’s okay. We just threw it in to be thorough.
If the date is later than today we call the DateDiff function to find out how much later:
DateDiff("d", dtToday, dtDate)
We pass DateDiff three parameters:
| • | “d” This first parameter specifies the interval we’re comparing. The “d” means we’re comparing days. |
| • | dtToday The first date in our comparison (in this case, today’s date) |
| • | dtDate The second date in our comparison (the date entered at the command line) |
Because we specified “d” as the interval, in this case DateDiff will return the number of days between the two dates. This just happens to be what we want for our first line of output, so we put the DateDiff call directly into our Wscript.Echo statement:
Wscript.Echo "Days: " & DateDiff("d", dtToday, dtDate)
Next we determine the number of months between the two dates. We can once again use the DateDiff function for this:
iMonth = DateDiff("m", dtToday, dtDate)
Notice that this time we pass DateDiff an “m” as the first parameter, meaning we want to retrieve the date interval in months. We assigned the output to the variable iMonth, then, because the number of months between the two dates is another piece of data we need to output for this event, we echo that value to the screen:
Wscript.Echo "Months: " & iMonth
Couldn’t we have simply put the DateDiff function call right into the Wscript.Echo statement, like we did when we were retrieving the number of days? Certainly. The only reason we didn’t is because we want to hang on to the months value; we’re going to need it again in just a moment.
So far this has been pretty easy. Now comes the tricky part of the event. We start by comparing the day part of the two dates:
If Day(dtDate) < Day(dtToday) Then
We use the Day function to retrieve the day portion of a date. For example, if the date is February 23, 2008, the Day function will return 23. If the day of the input date is less than the day today, we subtract 1 from the number of months between the two dates. Here’s why we do that:
Suppose today is February 27, 2008 and we entered a date of March 3, 2008. The number of months returned by DateDiff will be 1. However, the fact that the 27th is less than the 3rd tells us there’s not an entire month between these two dates. That’s why we subtract 1 from the number of months:
iMonth = iMonth – 1
The next thing we do is call the DateAdd function:
dtTemp = DateAdd("m", iMonth, dtToday)
DateAdd takes three parameters:
| • | “m” The date interval we want to work with, in this case months. |
| • | iMonth The number of months to add to the date. |
| • | dtToday The date we want to add to. |
You might be wondering why we’re adding to today’s date. Well, we already know how many months are between the two dates. By adding that number of months to today’s date, we can simply call the Days function again to retrieve the number of days between this new date (stored in dtTemp) and the input date (dtDate):
DateDiff("d", dtTemp, dtDate)
We then put the whole thing into a Wscript.Echo statement to echo the months plus days between today and the date entered:
Wscript.Echo "Month/Day: " & (iMonth) & " / " & DateDiff("d", dtTemp, dtDate)
If today was January 28, 2008, and we entered a date of May 18, 2009, this would be our output:
Days: 476 Months: 16 Month/Day: 15 / 20