A Little Calendar App I’m building

by


 Posted
  Comments
 Catagories: Web, and Coding


Unknown

The following code is a calendar application I’m building in JavaScript. It needs a couple rounds of refactoring (to get rid of document.writes and to make it more object friendly).  The next step will be to make it handle iCal files (which will be a trick).  This is posted for all to enjoy. See the link at the end of this document.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<pre>&lt;style&gt;
  body {
   font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
  }
  .calendar{
   background-color: #F5F5F5;
  }
  
  .EndOfMonth {
   background-color: #ADD8E6;
   padding: 0 .1em 0 .1em;
  }
  .EndOfYear {
   background-color: Green;
   padding: 0 .1em 0 .1em;
  }
  .NotEndOfMonth {
   padding: 0 .1em 0 .1em;
  }
  .NotEndOfYear {
   padding: 0 .1em 0 .1em;
  }
  .today {
   background-color: #E6E6FA;
  }
  .weekend {
   color: Silver;
   font-size: .75em;
  }
  .calendarDate {
   width: 30px;
   padding: .25em 0em .25em 0em;
   margin: .25em 0em .25em 0em;
   text-align: center;
  }
  .calendarheader {
   width: 30px;
   padding: .25em 0em .25em 0em;
   margin: .25em 0em .25em 0em;
   text-align: center;
   background-color: Gray;
   color: White;
   font-weight: bold;
   text-transform: uppercase;
  }
  .calendarheader .weekend {
   width: 30px;
   padding: .25em 0em .25em 0em;
   margin: .25em 0em .25em 0em;
   text-align: center;
   background-color: silver;
   color: White;
   font-weight: bold;
   text-transform: lowercase;
  }
  .Notes {
   text-align: left;
   font-weight: normal;
  }
  .NotesMonth {
   background-color: #ADD8E6;
  }
  
 &lt;/style&gt;
&lt;script&gt;
 sunday = 0;
 monday = 1;
 tuesday = 2;
 wednesday = 3;
 thursday = 4;
 friday = 5;
 saturday = 6; 
 
 /* Convert to Date Properity */
 var Month = new Array();
 Month[0] = "Janurary";
 Month[1] = "Feburary";
 Month[2] = "March";
 Month[3] = "April";
 Month[4] = "May";
 Month[5] = "June";
 Month[6] = "July";
 Month[7] = "August";
 Month[8] = "September";
 Month[9] = "October";
 Month[10] = "November";
 Month[11] = "December";
 
 
 function startCalendar(date) {
  document.write('&lt;table class="calendar" cellpadding="0" cellspacing="0"&gt;&lt;tr class="calendarheader"&gt;');
  document.write('&lt;td class="Notes"&gt;Month&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="weekend sunday weekend" id="sunday" title="Sunday" abbr="Sunday"&gt;s&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="monday" id="monday" title="Monday" abbr="Monday"&gt;m&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="tuesday" id="tuesday" title="Tuesday" abbr="Tuesday"&gt;t&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="wednesday" id="wednesday" title="Wednesday" abbr="Wednesday"&gt;w&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="thursday" id="thursday" title="Thursday" abbr="Thursday"&gt;&thorn;&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="friday" id="friday" title="Friday" abbr="Friday"&gt;f&lt;/td&gt;');
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
  document.write('&lt;td class="weekend saturday weekend" title="Saturday" id="saturday" abbr="Saturday"&gt;s&lt;/td&gt;');
 } 
 function finishCalendar(date) {
  document.write('&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;');
 } 
 function output_calendar_day(date) {
  var outputclass = " calendarDate ";
  /* Output a day in a calendar */
  if(date.getDay()==sunday || date.getDay()==saturday ) {
   outputclass = outputclass + " weekend ";
  }
  if(date.getDay()==sunday) {
   outputclass = outputclass + " sunday ";
  }
  if(date.getDay()==monday) {
   outputclass = outputclass + " monday ";
  }
  if(date.getDay()==tuesday) {
   outputclass = outputclass + " tuesday ";
  }
  if(date.getDay()==wednesday) {
   outputclass = outputclass + " wednesday ";
  }
  if(date.getDay()==thursday) {
   outputclass = outputclass + " thursday ";
  }
  if(date.getDay()==friday) {
   outputclass = outputclass + " friday ";
  }
  if(date.getDay()==saturday) {
   outputclass = outputclass + " saturday ";
  }
  if (date.toDateString() == today.toDateString() ){
   outputclass = outputclass + " today ";
  }
  document.write('&lt;td title="' + date.toLocaleDateString() +'" class="'+outputclass+'"&gt;'+date.getDate()+"&lt;/td&gt;");
 }
 function output_end_of_week(date) {
  /* Handle the End of the week */
  document.write('&lt;/tr&gt;&lt;tr&gt;&lt;td class="Notes"&gt;&lt;/td&gt;');
 }
 function output_end_of_month(date) {
  /* Handle the output for the end of the Month */
  document.write('&lt;td class="EndOfMonth" id="'+date.getYear()+Month[date.getMonth()]+'" title="'+Month[date.getMonth()]+'"&gt;&lt;/td&gt;');
  var thisMonth=date.getYear()+Month[date.getMonth()];
  d=document.getElementById(thisMonth);
  d.parentNode.childNodes[0].innerHTML = Month[date.getMonth()];
  d.parentNode.childNodes[0].className = d.parentNode.childNodes[0] + " NotesMonth ";
 }
 function output_not_end_of_month(date) {
  /* Handle the output for the end of the Month */
  document.write('&lt;td class="NotEndOfMonth"&gt;&lt;/td&gt;');
 }
 function output_end_of_year(date) {
  /* Handle the output for the end of the Month */
  document.write('&lt;td class="EndOfYear" title="'+date.getYear()+'"&gt;&lt;/td&gt;');
 }
 function output_not_end_of_year(date) {
  /* Handle the output for the end of the Month */
  document.write('&lt;td class="NotEndOfYear"&gt;&lt;/td&gt;');
 }
 var oneDay = (60 * 60 * 24 * 1000)
 today = new Date();
 start = new Date();
 finish = new Date();
 DateOutput = new Date();
 DateOutputYesterday = new Date();
 start.setMonth(today.getMonth()-3);
 finish.setMonth(today.getMonth()+9);
 start.setDate(start.getDate() - start.getDay());
 startCalendar(start);
 for(i = start.valueOf(); i &lt; finish.valueOf(); i=i+oneDay) {
  DateOutput.setTime(i)
  DateOutputYesterday.setTime(i-oneDay);
  if (DateOutputYesterday.getDay()==6) {
   output_end_of_week(DateOutput);  
  }
  if (DateOutput.getYear() !== DateOutputYesterday.getYear()) {
   output_end_of_year(DateOutput);
  }else{
   output_not_end_of_year(DateOutput);
  }
  if (DateOutput.getMonth() !== DateOutputYesterday.getMonth()) {
   output_end_of_month(DateOutput);
  }else{
   output_not_end_of_month(DateOutput);
  }
  output_calendar_day(DateOutput);
 }
 finishCalendar(finish);
&lt;/script&gt;</pre>

 

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.<!–

–>





Blog Search