Completing Calendars with VBScript Date Functions - Completing the code
(Page 3 of 4 )
At this point we have a complete working script. If you would like to learn how to add validation to this code, be sure to continue on to the next page. In the meantime, here’s what the completed script currently looks 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))
arrThisMonth = BuildArray()
WScript.Echo " ", MonthName(intMonth), intYear
WScript.Echo ""
WScript.Echo " Sun", " Mon", " Tue", " Wed", " Thu", " Fri", " Sat"
For i = 0 To UBound(arrThisMonth) Step 7
WScript.Echo arrThisMonth(i), arrThisMonth(i + 1), arrThisMonth(i + 2), arrThisMonth(i + 3), arrThisMonth(i + 4), arrThisMonth(i + 5), arrThisMonth(i + 6)
Next
Function BuildArray()
arrMonth = Array()
intEmptyDays = intFirstWeekDay - 1
For i = 1 To intEmptyDays
ReDim Preserve arrMonth(UBound(arrMonth) + 1)
arrMonth(UBound(arrMonth)) = " "
Next
For i = 1 To intDaysInMonth
ReDim Preserve arrMonth(UBound(arrMonth) + 1)
strDay = CStr(i)
intPad = 6 - Len(strDay)
strDay = Space(intPad) & strDay
arrMonth(UBound(arrMonth)) = strDay
Next
intEmptyDays = 7 - intLastWeekDay
For i = 1 To intEmptyDays
ReDim Preserve arrMonth(UBound(arrMonth) + 1)
arrMonth(UBound(arrMonth)) = " "
Next
BuildArray = arrMonth
End 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
Next: Validating user input >>
More Windows Scripting Articles
More By Nilpo