-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonth.js
More file actions
130 lines (103 loc) · 2.6 KB
/
Copy pathmonth.js
File metadata and controls
130 lines (103 loc) · 2.6 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
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
function getDate(year, month, day) {
return new Date(Date.UTC(year, month, day));
}
function addPreviousDays (list, i, n, month, year) {
for(; i <= n ; i++ ) list.push({
date: i,
month: month,
year: year,
previous: true
});
}
function addNextDays (list, i, n, month, year) {
for(; i <= n ; i++ ) list.push({
date: i,
month: month,
year: year,
next: true
});
}
function monthInformation(year, month, m, start_day){
m = m || {};
var i, n,
d = getDate(year, month, 1),
previous_month_d = getDate(year, month, 0),
month_last_day = getDate(year, month + 1, 0),
next_month_d = getDate(year, month + 1, 1);
start_day = start_day || 0;
m.start_day = start_day;
var last_day = previous_month_d.getUTCDate();
var last_week_day = previous_month_d.getUTCDay();
m.meta = {
date: d,
last_day: last_day,
last_week_day: last_week_day,
week_day: d.getUTCDay(),
month_last_day: month_last_day
};
m.year = d.getUTCFullYear();
m.month = d.getUTCMonth();
var list = [], lastMonthFirstWeekDay = last_day - last_week_day + start_day;
// previous month days
addPreviousDays(
list,
lastMonthFirstWeekDay,
last_week_day - start_day + lastMonthFirstWeekDay,
previous_month_d.getUTCMonth(),
previous_month_d.getUTCFullYear()
);
// current month days
for( i = 1, n = month_last_day.getUTCDate() ; i <= n ; i++ ) list.push({
date: i,
month: month,
year: year,
current: true
});
// next month days
addNextDays(
list,
1,
42 - list.length,
next_month_d.getUTCMonth(),
next_month_d.getUTCFullYear()
);
m.days = list;
return m;
}
var default_start_day = 0;
function Month (year, month, start_day) {
if( month < 0 ) {
month += 12;
year -= 1;
} else if( month > 11 ) {
month -= 12;
year += 1;
}
if( year instanceof Date ) {
month = year.getUTCMonth();
year = year.getUTCFullYear();
}
monthInformation(year, month, this, start_day || default_start_day);
}
Month.options = {};
Month.setFirstWeekDay = function (start_day) {
default_start_day = start_day;
};
Month.prototype.previous = function () {
return new Month(this.year, this.month - 1, this.start_day);
};
Month.prototype.next = function () {
return new Month(this.year, this.month + 1, this.start_day);
};
Month.prototype.getColumns = function() {
var columns = [], column;
for( var c = 0; c < 7; c++ ) {
column = [];
columns.push(column);
for( var r = 0; r < 6; r++ ) {
column.push( this.days[ r*7 + c ] );
}
}
return columns;
};
module.exports = Month;