Results 1 to 6 of 6

Thread: Calendar in VBScript

  1. #1
    Join Date
    Sep 2010
    Posts
    73

    Calendar in VBScript

    It may be helpful to put a timetable on a page that linked to other VBScript functions, can be extended to applications such as an agenda or a note card through the web, etc.. Let's start by defining what day of the week is the first of the month. To do this just use:
    firstofmonth= "01 /" & month (date ()) & "/" & Year (Date ())
    days WeekDay = (days)

    The goal is to have a table in a row and seven columns: If the first of the month is Sunday, the day will be one in the first cell, if it is a Monday, in the second, and so to follow. To do this, you construct a loop that generates empty cells until release day at the day of the week which they correspond.

    HTML Code:
    <HTML> 
    <HEAD> <TITLE> Exercise 2 </TITLE> 
    </HEAD> 
    <BODY> 
    <table border=1> 
    <tr> 
    <SCRIPT Language=vbscript> 
    <! - 
    firstofmonth = "01 /" & month (date ()) & "/" & Year (Date ()) 
    setdays WeekDay = (firstofmonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    day = 1 
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    next 
    //--> 
    </SCRIPT> 
    </Tr> 
    </Table> 
    </BODY> 
    </HTML>

  2. #2
    Join Date
    Sep 2010
    Posts
    73

    Re: Calendar in VBScript

    We spend the next lines. The variable day indicates the day that we arrived, we just have to make the same cycle in a new line.
    HTML Code:
    <HTML> 
    <HEAD> <TITLE> Exercise 2 </TITLE> 
    </ HEAD> 
    <BODY> 
    <table border=1> 
    <tr> 
    <SCRIPT Language=vbscript> 
    <! - 
    firstofmonth = "01 /" & month (date ()) & "/" & Year (Date ()) 
    setdays WeekDay = (firstofmonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    day = 1 
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    next 
    //--> 
    </SCRIPT> 
    </Tr> 
    </Table> 
    </BODY> 
    </HTML>
    Before continuing, we try to generalize the code, which basically is nothing but a repetition of the same rows to 4 times:

    HTML Code:
    <HTML> 
    <HEAD> 
    <TITLE> Exercise 2 </TITLE> 
    </HEAD> 
    <BODY> 
    
    <SCRIPT Language=vbscript> 
    <! - 
    document.write ("<table border=1>) 
    dayormonth = "01 /" & month (date ()) & "/" & Year (Date ()) 
    day = 1 
    for cycle = 1 to 4 
    document.write ("<tr>) 
    setdays WeekDay = (dayormonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    next 
    document.write ("</ tr>") 
    dayormonth = (day) & "/" & month (date ()) & "/" & Year (Date ()) 
    next 
    document.write ("</ table>") 
    //--> 
    </SCRIPT> 
    </BODY> 
    </HTML>

  3. #3
    Join Date
    Sep 2010
    Posts
    73

    Re: Calendar in VBScript

    How often should the cycle be repeated? It depends on how many days of the month and that day is the first of the month. Thinking a bit ', ranging from 4 to 6 cycles. To determine the end of the month, you can use this trick: from the first of next month takes off one day
    firstofmonthfollowing= "01" & "/" & (month (date ()) +1) & "/" & Year (Date ())
    lastmonth= DateAdd ("d", -1, firstofmonthfollowing)
    Now we just have to continue the cycle until the day variable is less than the last of the month:
    HTML Code:
    <HTML> 
    <HEAD> 
    <TITLE> Exercise 2 </ TITLE> 
    </ HEAD> 
    <BODY> 
    
    <script type="text/vbscript"> 
    <! - 
    document.write ("<table border=1>) 
    dayormonth = "01 /" & month (date ()) & "/" & Year (Date ()) 
    day = 1 
    firstofmonthnext = "01" & "/" & (month (date ()) +1) & "/" & Year (Date ()) 
    ofmonth = DateAdd ("d", -1, firstofmonthnext) 
    While (day <day (ofmonth)) 
    document.write ("<tr>) 
    setdays WeekDay = (dayormonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    next 
    document.write ("</ tr>") 
    dayormonth = (day) & "/" & month (date ()) & "/" & Year (Date ()) 
    Wend 
    document.write ("</ table>") 
    //--> 
    </ SCRIPT> 
    </ BODY> 
    </ HTML> 

  4. #4
    Join Date
    Sep 2010
    Posts
    73

    Re: Calendar in VBScript

    We just have to stop writing days at the end of the month and beautify all:
    Code:
    <script type="text/vbscript"> 
    <! - 
    document.write ("<table border=1> class="table") 
    dayormonth = "01 /" & month (date ()) & "/" & Year (Date ()) 
    day = 1 
    firstofmonthnext = "01" & "/" & (month (date ()) +1) & "/" & Year (Date ()) 
    ofmonth = DateAdd ("d", -1, firstofmonthnext) 
    While (day <day (ofmonth)) 
    document.write ("<tr>) 
    setdays WeekDay = (dayormonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    next 
    document.write ("</ tr>") 
    dayormonth = (day) & "/" & month (date ()) & "/" & Year (Date ()) 
    Wend 
    document.write ("</ table>") 
    //--> 
    </ Script>
    Code:
    <script type="text/vbscript"> 
    <! - 
    document.write ("<table border=1>) 
    document.write ("<tr> <td align=center colspan=7>" & 
    Ucase (monthname (month (date ()))) & "" & Year (Date ()) & "</ td> </ tr>") 
    document.write 
    (<tr> <td> D </ td> <td> L </ td> <td> M </ td> <td> M </ td> <td> G </ td> <td> V < / td> <td> S < 
    ; / Td> </ tr> ") dayormonth =" 01 / "& month (date ()) &" / "& Year (Date ()) 
    day = 1 
    'This control solves the Case Study of December 
    if (month (date ()) +1)> 12 then 
    year = Year (date ()) +1 
    month = 1 
    else 
    year = Year (Date ()) 
    month = (month (date ()) +1) 
    end if 
    firstofmonthnext = "01" & "/" & month & "/" & Year 
    ofmonth = DateAdd ("d", -1, firstofmonthnext) 
    While (day <day (ofmonth)) 
    document.write ("<tr>) 
    setdays WeekDay = (dayormonth) 
    for i = 1 to setdays-1 
    document.write ("<td> </ td>") 
    next 
    
    
    for i = setdays to 7 
    document.write ("<td>" & days & "</ td>") 
    day = day +1 
    if day> day (ofmonth) then exit for 
    next 
    for i = i +1 to 7 
    document.write ("<td> </ td>") 
    next 
    document.write ("</ tr>") 
    dayormonth = (day) & "/" & month (date ()) & "/" & Year (Date ()) 
    Wend 
    document.write ("</table>") 
    //--> 
    </SCRIPT>
    For those who love new technology, in ASP.NET a timetable similar to the one just created is done by calling an object.

  5. #5
    Join Date
    Sep 2011
    Posts
    1

    Re: Calendar in VBScript

    hi. could you please help me in displaying the calendar in vbscript without html. I will just write the code in notepad and save it as .vbs extension.
    The thing is i want a program to display the calendar based on the user inputs. If a user enters the any year and any month of that year, calendar of that month should display. Could you please help me.

  6. #6
    Join Date
    Jan 2006
    Posts
    605

    Re: Calendar in VBScript

    I have made one VBScript calendar but its in html:

    Code:
    <%
    
    Dim currDate, nMonth, nDay, nYear
    Dim lastMonth, lastYear, lastFirstDayPos, lastNumDays, submit
    Dim numDays, monthName(13), count, pos, firstDayPos, eventDay, dayNum, cell(38)
    Dim events(38)
    Dim test1, test2, test3 ' used in leap year calcs
    
    If Request.Form("submit") <> "" Then
    
    lastMonth = Int(Request.Form("lastMonth"))
    lastYear = Int(Request.Form("lastYear"))
    lastFirstDayPos = Int(Request.Form("lastFirstDayPos"))
    lastNumDays = Int(Request.Form("lastNumDays"))
    submit = Request.Form("submit")
    
    End If
    
    currDate = Date ' Date contains the current system date.
    nMonth = Int(Month(currDate)) ' integer value 1 to 12
    nDay = Int(Day(currDate)) ' integer value 1 to 31
    nYear = Int(Year(currDate)) ' integer value yyyy
    dayOfWeek = Int(Weekday(currDate)) ' integer values 1 to 7 (note diff from mini-SQL 0-6)
    
    %>
    
    <HTML>
    <HEAD>
    <TITLE>Calendar</TITLE>
    </HEAD>
    <BODY BGCOLOR="#ffffff" TEXT="#00000">
    <CENTER>
    <TABLE BORDER CELLPADDING="2">
    
    <%
    
    ' Previous or Next Month (passed values) 
    
    If submit = "Previous Month" Then
    
    nMonth = lastMonth - 1
    nYear = lastYear -1
    If nMonth = 0 Then
    nYear = nYear - 1
    nMonth = 12
    
    End If
    
    If submit = "Previous Month" Then
    
    nMonth = lastMonth - 1
    nYear = lastYear -1
    If nMonth = 0 Then
    nYear = nYear - 1
    nMonth = 12
    
    End If
    
    If submit = "Next Month" Then
    
    nMonth = lastMonth + 1
    nYear = lastYear
    If nMonth = 13 Then
    nYear = nYear + 1
    nMonth = 1
    
    End If
    
    ' Number of days of month 30 or 31, with special case of February (28 or 29) 
    
    If nMonth = 4 Or nMonth = 6 Or nMonth = 9 Or nMonth = 11 Then
              numDays = 30
    ElseIf nMonth <> 2 Then
              numDays = 31
    End If
    
    If nMonth = 2 Then
    
    ' Leap year calculations:
    ' All years that are evenly divisible by 400 or are evenly 
    ' divisible by 4 and NOT divisible by 100 are leap years
    
    test1 = nYear Mod 400 
    If test1 = 0 Then
              numDays = 29
    Else
              test2 = nYear Mod 4
              test3 = nYear Mod 100
              If test2 = 0 And test3 > 0 Then
                        numDays = 29
              Else
                        numDays = 28
              End If
    End If
    
    End If
    
    ' Make month name array 
    
    monthName(1) = "January"
    monthName(2) = "February"
    monthName(3) = "March"
    monthName(4) = "April"
    monthName(5) = "May"
    monthName(6) = "June"
    monthName(7) = "July"
    monthName(8) = "August"
    monthName(9) = "September"
    monthName(10) = "October"
    monthName(11) = "November"
    monthName(12) = "December"
    
    ' Find day of week (0-6) of first day of month. firstDayPos will
    ' help determine later display of cells. 
    
    If nMonth = Month(currDate) Then
    
              ' Current Month
    
    count = nDay
    pos = dayOfWeek - 1 ' subtract one for VB offset
    While count > 0
              If count = 1 Then ' must come before pos switch to preserve assignment
                        firstDayPos = pos
              End If
              If pos = 0 Then ' must come before decrement, since Sunday = 0 
                        pos = 7 ' must be 7 so that next loop is 6 
              End If
              count = count - 1
              pos = pos - 1 
    Wend
    
    End If
    
    ' Get new first day position depending on whether prev or next month 
    ' has 31, 30, 29, or 28 days. Cannot be calculated from current
    ' time. 
    
    If submit = "Previous Month" Then
    
    firstDayPos = lastFirstDayPos - numDays + 28
    If firstDayPos < 0 Then
              firstDayPos = firstDayPos + 7
    End If
    
    End If
    
     If submit = "Next Month" Then
    
    firstDayPos = lastFirstDayPos + lastNumDays - 28
    If firstDayPos > 6 Then
              firstDayPos = firstDayPos - 7
    End If
    
    End If
    
    ' Prepare events for insertion
    
    eventDay = Day(currDate)
    
    ' Make cell array 
    
    count = 0
    dayNum = 1
    While count <= numDays + firstDayPos ' Add number of initial blank days to count
    
    If count < firstDayPos Then
              ' Empty cells display as solid blank
              cell(count) = "<td></td>"
    Else
              ' Create empty event array
    
    events(dayNum) = ""
    If nMonth = Month(currDate) And nYear = Year(currDate) And eventDay = dayNum Then
              ' Assign check to day
              events(eventDay) = "<IMG SRC=" & Chr(34) & "check7.gif" & Chr(34) & ">"
    End If
    cell(count) = "<TD align=left valign=top>" & dayNum & "" & events(dayNum) _
              & "</td>"
    dayNum = dayNum + 1
    
    End If
    count = count + 1
    
    Wend
    
    ' Print the calendar elements. Table tags are above and below this in the HTML.
    ' Beginning first row tag and ending last row tag are outside the loop. 
    
    Response.Write "<CAPTION><FONT size=5>" & monthName(nMonth) & " " & nYear & "</FONT></CAPTION>"
    Response.Write "<TR>"
    Response.Write "<TD align=center> Sunday </TD>"
    Response.Write "<TD align=center> Monday </TD>"
    Response.Write "<TD align=center> Tuesday </TD>"
    Response.Write "<TD align=center> Wednesday </TD>"
    Response.Write "<TD align=center> Thursday </TD>"
    Response.Write "<TD align=center> Friday </TD>"
    Response.Write "<TD align=center> Saturday </TD>"
    Response.Write "</TR>"
    Response.Write "<TR>"
    
    count = 0
    While count < numDays + firstDayPos
    
    Response.Write "" & cell(count)
    
    ' Insert subsequent table row tags in appropriate place after cell print 
    
    If count = 6 Or count = 13 Or count = 20 Or count = 27 Or count = 34 Then
    
    Response.Write "<TR></TR>" ' Empty initial cells will display solid blank
    
    End If
    
    count = count + 1
    
    ' Final unprinted empty cells will display solid blank
    
    Wend
    
    Response.Write "</TR></TABLE></CENTER>"
    Response.Write "<center><form action=" & Chr(34) & "calendar.asp" & Chr(34) & "method=""post"">"
    Response.Write "<input type=hidden name=lastMonth value=" & nMonth & ">"
    Response.Write "<input type=hidden name=lastYear value=" & nYear & ">"
    Response.Write "<input type=hidden name=lastFirstDayPos value=" & firstDayPos & ">"
    Response.Write "<input type=hidden name=lastNumDays value=" & numDays & ">"
    Response.Write "<input type=submit name=submit value=" & Chr(34) & "Previous Month" _
              & Chr(34) & ">"
    Response.Write "<input type=submit name=submit value=" & Chr(34) & "Next Month" _
              & Chr(34) & "></center>"
    
    %>
    
     <p>This calendar uses NT system clock values to enter at the
    current month and to place a check mark at the current day.
    The "Previous Month" and "Next Month" submit buttons then
    resubmit values from which the previous or next month's calendar is
    calculated. The script is written in VBScript Active Server Pages (ASP),
    translated from mini-SQL's Lite scripting language.</p>
    
    <p align="center">
    
    <%
    
    Response.Write "You are using "
    Response.Write ScriptEngine
    Response.Write " version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion
    Response.Write "&nbsp;&nbsp;&nbsp; Date: " & currDate
    
    %>
    
    </p>
    </BODY>
    </HTML>

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2012, 12:24 PM
  2. Replies: 2
    Last Post: 07-02-2012, 07:50 PM
  3. Replies: 5
    Last Post: 23-04-2011, 10:19 PM
  4. Replies: 3
    Last Post: 18-10-2010, 12:12 PM
  5. Replies: 9
    Last Post: 25-10-2009, 01:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,285,551.51796 seconds with 17 queries