Completing Calendars with VBScript Date Functions - Building the array
(Page 2 of 4 )
Now I can create a loop to add the correct number of empty values to the beginning of my array. I’m just adding spaces as strings. You can see the columns in my example output allow three spaces to display a given date. I’m also padding that with three leading spaces to space out my columns a little more and make it look pretty. So each empty date should contain a string of six spaces.
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
Next I add the individual days of the month to my array by looping through each value between 1 and the last day of the month. Notice again that I’m padding each value as necessary so that each date string contains six characters.
intEmptyDays = 7 - intLastWeekDay
For i = 1 To intEmptyDays
ReDim Preserve arrMonth(UBound(arrMonth) + 1)
arrMonth(UBound(arrMonth)) = " "
Next
Once all the dates are in place, I’m finding the number of empty days at the end of the month. This is done by taking the highest possible weekday, 7 for Saturday, and subtracting the weekday of the last day in the month. These are added to the array as strings of spaces as before. Adding these empty days ensures that our array contains complete weeks.
BuildArray = arrMonth
End Function
Finally, an array of complete weeks for our given month is returned to the main script.
Back in our main script, we’re ready to begin displaying the calendar.
WScript.Echo " ", MonthName(intMonth), intYear
WScript.Echo ""
WScript.Echo " Sun", " Mon", " Tue", " Wed", " Thu", " Fri", " Sat"
A few lines are used to set up the basic look by providing the month and year titles and well as the weekday headers for our table.
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
Next, we rely on a For loop to move through the values in our array of days. We want to return them in one-week intervals so that each week can be printed on a new line. To do this we simply move through the entire array with a Step value of 7, essentially reading seven values at a time. Then it’s just a matter of printing each of those seven values on a single line.
Next: Completing the code >>
More Windows Scripting Articles
More By Nilpo