By Samuel Pickard
There are many systems that I can think of which would benefit from having a shared calendar into which users can log and read events. Almost any group based project, whether running on an intranet or over the Internet could make use of it. So, following Matthew Reynolds' suggestion, I wrote some code to produce it.
Supporting database structure
For our event calendar, we need an extremely simple database. In fact it only has one table and three fields.
Table: Events
EventID Integer
EventText Text (or Memo)
EventDate DateTime
I have built my database in SQL Server 6.5, but you could use any system with ODBC drivers. I have included the schema for download.
Handling dates across different date systems When I was planning this project, one of my biggest factors was that it dealt with dates, but would have to work on anyone's server. I am sure that I am not alone when I get frustrated when I have to use a date format which is not native to me. Fortunately, Microsoft have put a lot of work into date handling, and have made VBScript as format independent as is reasonable. With all the date functions in VBScript, you can specify the date that you want to consider the first day of the week. For example
WeekDay(date, vbSunday)
This line will tell which day of the week the given date will fall, starting from Sunday. The default however is
WeekDay(date, 0)
Which will tell you the day of the week, based on the date settings on the server. For this project, all dates are linked to the server settings.
Rendering your calendar to screen Probably the hardest part of this project is to get your monthly calendar view onto the screen. Finding the current date is easy. VBScript provides a function called Date() which returns the current system date. From there, the next step is also easy. As part of the date functions, VBScript also gives you
Month(
date)
and
Year(date)
both of which will extract the month and date parts from a date, based on the formats held on the server. One other useful function will also give you the name of the month.
MonthName(month)
Again, this is from the system settings. From MonthName(1) I will get 'January', but someone with a French server will get 'Janvier'.
From this, we can start the HTML Table that will hold our month view.
Easy. Now, what we want to do is to output the names of the days, independently of language or dating convention. Fortunatly, Microsoft has done the hard bits on this as well. The VBScript function WeekDayName provides this. You can pass it a day of the week, and it will return the name of that day. It will also return an abbreviated version of the name, so rather than it giving me 'Monday', it will give me 'Mon'.
<TR> <% For DayLoop = 1 to 7%> <TD><%= WeekDayName(Dayloop, True, 0)%></TD> <% Next%> </TR>
The order that the names are retreived in is also dependent on the language setting of the server. In the UK, my code will list the days from Monday to Sunday. A server running in the US will list them from Sunday to Saturday.
Ok. Now we are getting to the nitty-gritty. We need to put the correct date underneath the correct day. What we actually need is to know on which day of the week, the first day of that month falls.
Let's get some more information. We can use the VBScript function DateSerial to create a variable FirstDayDate. DateSerial will format the year, month and date and return it in date format. We have already defined CurrentYear and CurrentMonth earlier on. We want to use 1, because it is the first of the month we are after.
WeekDay will return the number of the day that corresponds to the server settings for the week order, and matches the week list we have already generated. For instance if Wednesday is the third day on your list, running WeekDay on a date that you know falls on a Wednesday will return 3.
We alse need CurrentDay as we move though the month.
We can now pad out the blanks at the beginning of calendar.
<% If FirstDay <> 1 Then %> <TDColspan="<%=FirstDay -1%>"> <% End if %>
We don't want any padding if the first day of the month is the same as the first day of the week. We also only want to pad upto the first day of the week, so we can span one table data element FirstDay-1 columns to bring us into line.
At the end of each week, we want to start a new row. I have created a variable which will keep track of how many days through the week we are. As we have already moved partway through the first week, we need to initialise it to a starting value.
DayCounter = FirstDay
We are also going to stop showing the days when we hit the end of the month.
CorrectMonth = True
Ok. Let's do the main loop.
<% Do While CorrectMonth = True %> <TD> <%=Day(CurrentDay)%> </TD>
<% DayCounter = DayCounter + 1
If DayCounter > 7 then DayCounter = 1%> </TR><TR> <% End if
CurrentDay = DateAdd("d", 1, CurrentDay)
If Month(CurrentDay) <> CurrentMonth then CorrectMonth = False End if Loop %>
This should be fairly simple, but let's step through it. We are only going to keep doing the loop whilst we are in the target month, so it is while CorrectMonth is True. We can write out each day to a new table data cell. Once we have done that we need to move DayCounter along. If we have done the seven days, we need to close the table row and start a new one. We also need to move then date variable onto the next day. To do this, you can use the DateAdd function which will add a time period ("d" for "day"), the number of units (1) and an original date. If by moving onto this day we are into the next month, set CurrentMonth to False and exit the loop.
Samuel Pickard is an old school client/server programmer who made the move from programming in C/ESQL on UNIX to the easy life writing ASP. He has produced many ASP projects over the last few years, but now specialises in creating on-line magazines. Samuel also trains a lot of new developers in ASP/ADO who promptly get tempted to go to London for 'a measly 100% payrise'. Samuel stays well clear of Site Server, and hopes one day to have a better dev server.
In' tra net
- n. 1) a computer network connecting an affiliated set of
clients using standard internet protocols, esp. TCP/IP and HTTP.
2) an IP-based network of nodes behind a firewall, or behind several
firewalls connected by secure, possibly virtual, networks.
IDM Unwired
IDM on the Road
NOW AN OFFICIAL AVANTGO CHANNEL!
To easily add IDM to your PDA click here