'
Introduction
This article focus on how to build Dynamic Calendar from scratch in easy steps so we can customize it for any new goal that may be requested in any project.
Background
Some Times telling what day could be task for even smarts one of people, and there is lot of ready tools build to help us with but, may someday someone ask you to build new calendar control from scratch so you I though how we can build one.
Using Code
To build any simple calendar we need to know several things about month we want show its table:
current month number and year number from this two knowns we get number of day's in current month at this year using function Date.DaysInMonth(year, month)
after that we generate an instance of date object with passed month and year with day number 1, using another property of Date Object we get current (1 day in month number in first week of that month) currentDate.DayOfWeek()
so we can get the offset in weeks array that contain days ordered from (Sun to Sat) so we can know how to represent our day as shown in code below.
Code
Function DrawCalender(ByVal month As Integer, ByVal year As Integer) As String<br /> 'to hold current date of this day for presentation peropse <br /> Dim today As Date = New Date(Now.Year, Now.Month, Now.Day)<br /> 'object of passed month and year<br /> Dim currentDate As Date = New Date(year, CInt(month), 1)<br /> 'array hold days of week<br /> Dim days() As String = New String() {"Sun", "Mon", "Tu", "Wen", "Thr", "Fri", "Sat"}<br /> 'temp holder to hold week name from array above<br /> Dim day As String<br /> 'number of days in selected month<br /> Dim numDays As Integer = Date.DaysInMonth(year, month)<br /> Dim index, position As Integer <br /> Dim returnData As String = ""<br /> '1st day of month day number<br /> Dim offset As Integer = currentDate.DayOfWeek()<br /> 'MsgBox(offset)<br /> returnData = "<table border=""0"" cellpadding=""0"" cellspacing=""0"">"<br /> returnData &= "<tr>"<br /> returnData &= "<td colspan=""7"" align=""center"">" & currentDate.Month.ToString() & " / " & currentDate.Year & "</td>"<br /> returnData &= "</tr>"<br /> returnData &= "<tr>"<br /> For Each day In days<br /> returnData &= "<td align=""center"" width=""26"">" & day & "</td>"<br /> Next<br /> returnData &= "</tr>"<br /> position = 0<br /> For index = 1 - offset To numDays<br /> If position Mod 7 = 0 Then<br /> returnData &= "<tr>"<br /> End If<br /> If index < 1 Then<br /> returnData &= "<td align=""center"">-</td>"<br /> ElseIf index = today.Day AndAlso year = today.Year AndAlso month = today.Month Then<br /> returnData &= "<td align=""center""><b>" & (index).ToString() & "</b></td>"<br /> Else<br /> returnData &= "<td align=""center"">" & (index).ToString() & "</td>"<br /> End If<br /> <br /> position += 1<br /> If position Mod 7 = 0 Then returnData &= "</tr>"<br /> Next<br /> <br /> If position Mod 7 <> 0 Then<br /> For index = 1 To 7 - (position Mod 7)<br /> returnData &= "<td align=""center"">-</td>"<br /> Next<br /> returnData &= "</tr>"<br /> End If<br /> returnData &= "</table>"<br /> Return returnData<br /> End Function<br /> Private Function Years() As String()<br /> Dim str(10) As String<br /> Dim index As Integer<br /> For index = 0 To str.Length - 1<br /> str(index) = Now.Year - str.Length + index + 1<br /> Next<br /> Return str<br /> End Function
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.