-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetCalendarMonthAsTable.js
More file actions
62 lines (52 loc) · 1.8 KB
/
getCalendarMonthAsTable.js
File metadata and controls
62 lines (52 loc) · 1.8 KB
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
/**
* Get HTML for selected year+month
*
* @param int year
* @param int month [0-11]
* @returns string table
*/
function getCalendarMonthAsTable(year, month, options) {
var day, i,
firstDay = new Date(year, month, 1).getDay(),
lastDate = new Date(year, month + 1, 0).getDate(),
output = [],
startDate = 1;
options = $.extend({
"firstDayOfWeek": 1, // Monday
"showWeekDays": true,
"weekDays": ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
}, options);
startDate = startDate - (firstDay - options.firstDayOfWeek);
if (startDate > 1) {
startDate = startDate - 7;
}
var cell = 0, week = 0;
for (day = startDate; day <= lastDate; day++, td++) {
week = Math.floor(cell++ / 7);
if (!output[week]) {
output[week] = [];
}
if (day > 0) {
output[week].push('<td data-date="' + getFullDateAsNumber(year, month, day) + '">' + day + '</td>');
} else {
output[week].push('<td></td>');
}
}
for (i = 0; i < output.length; i++) {
output[i] = '<tr>' + output[i].join('') + '</tr>';
}
if (options.showWeekDays) {
output.head = [];
for (i = 0; i < 7; i++) {
output.head.push('<th>' + options.weekDays[(options.firstDayOfWeek + i) % 7] + '</th>');
}
}
output.body = '<tbody>' + output.join('') + '</tbody>';
output.head = output.head ? '<thead><tr>' + output.head.join('') + '</tr></thead>' : '';
return '<table>' + output.head + output.body + '</table>';
function getFullDateAsNumber(year, month, day) {
var date = ('0000' + year).substr(-4) + ('00' + (month + 1)).substr(-2) + ('00' + day).substr(-2);
return parseInt(date);
}
}
getCalendarMonthAsTable(2015, 9);