Building Calendars with VBScript Date Functions - Calculating the remaining values
(Page 4 of 4 )
Next we need to calculate some more values. Let’s think for a minute about the types of values that are actually needed to create a calendar. First, you obviously need a month and year—which we have. Next, you need to know how many days are in that month. Then, you need to know on which day of the week the first appears. Finally, although it’s not completely necessary, it’s also helpful to know on what weekday the last day of the month falls.
dteFirstDay = DateSerial(intYear, intMonth, 1)
intFirstWeekDay = WeekDay(dteFirstDay)
intDaysInMonth = DaysInMonth(intMonth, intYear)
intLastWeekDay = WeekDay(DateSerial(intYear, intMonth, intDaysInMonth))
VBScript’s DateSerial function allows us to easily return a Date type for the first day of the month using the string parts that we have already. Supplying a day value of 1 obviously returns the first of that month. The WeekDay function quickly tells us on what weekday that occurs.
We’ll create a simple function called DaysInMonth to determine the number of days in a month. Quite simply, it finds the last day of the month. As it turns out, this is an Integer representation of the number of days in a month. We can then use this value to return a Date for the last day of the month. Let’s take a quick look at that function.
Function DaysInMonth(intMonth, intYear)
If intMonth = 12 Then
dteNextMonth = DateSerial(intYear + 1, 1, 1)
Else
dteNextMonth = DateSerial(intYear, intMonth + 1, 1)
End If
DaysInMonth = DateDiff("d", dteFirstDay, dteNextMonth)
End Function
This function very simply finds the first day of the following month by incrementing the month value. Subtracting a single day from this number will return the last day of the month we are working with. Notice that the following month for December is actually January in the next year. A simple If statement can handle this case. If the supplied month is 12, or December, I increment that year instead and use January 1.
I then use the DateDiff function to return the integer number of days between the first day of the current month and the first day of the following month. You could just as easily do this using the DateAdd function.
DaysInMonth = Day(DateAdd("d", -1, dteNextMonth))
I could have used the DateAdd function to move the first day of the following month back a single day making it the last day of the current month. The Day function could then be used to return that Integer value. You can use whichever method you like.
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then
strPath = WScript.ScriptFullName
strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34)
CreateObject("WScript.Shell").Run(strCommand)
WScript.Quit
End If
strDate = InputBox("Please enter a month and year as MM/YYYY.", "Microsoft Scripting Games 2008")
If strDate = "" Then WScript.Quit
arrDateParts = Split(strDate, "/")
intMonth = arrDateParts(0)
intYear = arrDateParts(1)
dteFirstDay = DateSerial(intYear, intMonth, 1)
intFirstWeekDay = WeekDay(dteFirstDay)
intDaysInMonth = DaysInMonth(intMonth, intYear)
intLastWeekDay = WeekDay(DateSerial(intYear, intMonth, intDaysInMonth))
Function DaysInMonth(intMonth, intYear)
If intMonth = 12 Then
dteNextMonth = DateSerial(intYear + 1, 1, 1)
Else
dteNextMonth = DateSerial(intYear, intMonth + 1, 1)
End If
DaysInMonth = DateDiff("d", dteFirstDay, dteNextMonth)
End Function
I’m out of space for this article. At this point, our script looks like this. We’ve begun our calendar script, calculated all of the necessary values, and now we’re ready to begin building our script’s output display. We’ll jump right into that in my next article.
If you want to get an early start, you can download the full code for this article here. Until next time, coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |