Dynamic Calendar

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
essential essential is offline Offline Aug 8th, 2008, 10:44 pm |
0
Making a calendar is a bit more complex than making a timer or clock but mostly because any calendar script is going to be generating HTML to display the calendar and whenever you have programs writing programs things always get a bit funky! We'll do this as a prototype so all you have to do is create a date with the month you want the calendar for and use the.calendar() method.
Quick reply to this message  
JavaScript / DHTML / AJAX Syntax
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Calendar</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <style type="text/css">
  7. <!--
  8. /* This is our current day marker */
  9.  
  10. #current_day { background-color:yellow; font-weight: bold; }
  11. -->
  12. </style>
  13. </head>
  14. <body>
  15. <script type="text/javascript">
  16. <!-- Begin Hiding
  17. var today = new Date();
  18. var month = today.getMonth();
  19. //Using the Date prototype to assign our month names-->
  20. Date.prototype.getMonthNames = function() { return [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; }
  21.  
  22. //Getting the number of day in the month.-->
  23. Date.prototype.getDaysInMonth = function()
  24. { return new Date( this.getFullYear(), this.getMonth() + 1, 0 ).getDate(); }
  25. Date.prototype.calendar = function()
  26. { var numberOfDays = this.getDaysInMonth();
  27. //This will be the starting day to our calendar-->
  28. var startingDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
  29. //We will build the calendar_table variable then pass what we build back-->
  30. var calendarTable = '<table summary="Calendar" class="calendar" style="text-align: center;">';
  31. calendarTable += '<caption>' + this.getMonthNames()[this.getMonth()] + '&nbsp;' + this.getFullYear() + '</caption>';
  32. calendarTable += '<tr><td colspan="7"></td></tr>';
  33. calendarTable += '<tr>';
  34. calendarTable += '<td><font color="#B42600">S</font></td>';
  35. calendarTable += '<td>M</td>';
  36. calendarTable += '<td>T</td>';
  37. calendarTable += '<td>W</td>';
  38. calendarTable += '<td>TH</td>';
  39. calendarTable += '<td>F</td>';
  40. calendarTable += '<td>S</td></tr>';
  41. //Lets create blank boxes until we get to the day which actually starts the month-->
  42. for ( var i = 0; i < startingDay; i++ )
  43. { calendarTable += '<td>&nbsp;</td>'; }
  44. //border is a counter, initialize it with the "blank"-->
  45. //days at the start of the calendar-->
  46. //Now each time we add new date we will do a modulus-->
  47. //7 and check for 0 (remainder of border/7 = 0)
  48. //if it's zero then it's time to make new row-->
  49. var border = startingDay;
  50. //For each day in the month, insert it into the calendar-->
  51. for ( var id = '', i = 1; i <= numberOfDays; i++ )
  52. { if (( month == month ) && ( today.getDate() == i )) { id = 'id="current_day"'; }
  53. else { id = ''; }
  54. calendarTable += '<td ' + id + '>' + i + '</td>'; border++;
  55. if ((( border % 7 ) == 0 ) && ( i < numberOfDays ))
  56. {
  57. //Time to make new row, if there are any days left.-->
  58. calendarTable += '<tr></tr>'; } }
  59. //All the days have been used up, so pad the empty days until the end of calendar-->
  60. while(( border++ % 7)!= 0)
  61. { calendarTable += '<td>&nbsp;</td>'; }
  62. //Finish the table-->
  63. calendarTable += '</table>';
  64. //Return it-->
  65. return calendarTable; }
  66. //--> Let's add up some dynamic effect
  67. window.onload = function() {
  68. selected_month = '<form name="month_holder">';
  69. selected_month += '<select id="month_items" size="1" onchange="month_picker();">';
  70. for ( var x = 0; x <= today.getMonthNames().length; x++ ) { selected_month += '<option value="' + today.getMonthNames()[x] + ' 1, ' + today.getFullYear() + '">' + today.getMonthNames()[x] + '</option>'; }
  71. selected_month += '</select></form>';
  72. actual_calendar = document.getElementById('show_calendar');
  73. actual_calendar.innerHTML = today.calendar();
  74. var month_listing = document.getElementById('current_month');
  75. month_listing.innerHTML = selected_month;
  76. actual_month = document.getElementById('month_items');
  77. actual_month.selectedIndex = month;
  78. }
  79. //--> Month Picker <--\\
  80. function month_picker()
  81. { month_menu = new Date(actual_month.value);
  82. actual_calendar.innerHTML = month_menu.calendar();
  83. }
  84. // Done Hiding -->
  85. </script>
  86. <p>&nbsp</p>
  87. <div id="show_calendar">&nbsp;</div>
  88. <div id="current_month">&nbsp;</div>
  89. </body>
  90. </html>
0
uringinteristi uringinteristi is offline Offline | Dec 30th, 2008
I am newbie . Can you help me to make moving text in javascript?
 
0
uringinteristi uringinteristi is offline Offline | Jan 27th, 2009
hi again ma brow
it can't use at my website
 
0
DealthRune DealthRune is offline Offline | Jun 18th, 2009
Neat =)
 
 

Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC