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
197
198
199
200
201
202
203
204
|
/*
* Calendar math built on jquery-global
*
* Based on Marc Grabanski's jQuery Date Plugin
* http://marcgrabanski.com/articles/jquery-date-plugin
*/
(function( $, undefined ) {
var weekdays = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ],
weekdaysRev = {
"sun": 0,
"mon": 1,
"tue": 2,
"wed": 3,
"thu": 4,
"fri": 5,
"sat": 6
};
Globalize.locale( "en" );
$.date = function( date, globalFormat ) {
if ( !( this instanceof $.date ) ) {
return new $.date( date, globalFormat );
}
if ( typeof date === "string" && date.length ) {
this.dateObject = Globalize.parseDate( date, globalFormat );
}
this.dateObject = this.dateObject || new Date();
this.globalFormat = globalFormat;
};
$.date.prototype = {
setFormat: function( format ) {
if ( format ) {
this.globalFormat = format;
}
return this;
},
//TODO: same as the underlying Date object's terminology, but still misleading.
//TODO: We can use .setTime() instead of new Date and rename to setTimestamp.
setTime: function( time ) {
this.dateObject = new Date( time );
return this;
},
setDay: function( day ) {
var date = this.dateObject;
this.dateObject = new Date( date.getFullYear(), date.getMonth(), day, date.getHours(),
date.getMinutes(), date.getSeconds() );
return this;
},
setMonth: function( month ) {
// Overflow example: Month is October 31 (yeah Halloween) and month is changed to April with 30 days,
// the new date will me May 1. We will honor the month the user wants to set and if and overflow
// occurs, set to last day of month.
var date = this.dateObject,
days = date.getDay(), year = date.getFullYear();
if ( days > this.daysInMonth( year, month ) ) {
// Overflow
days = this.daysInMonth( year, month );
}
this.dateObject = new Date( year, month, days, date.getHours(),
date.getMinutes(), date.getSeconds() );
return this;
},
setYear: function( year ) {
var date = this.dateObject,
day = date.getDate(),
month = date.getMonth();
// Check if Leap, and February and day is 29th
if ( this.isLeapYear( year ) && month == 1 && day == 29 ) {
// set day to last day of February
day = this.daysInMonth( year, month );
}
this.dateObject = new Date( year, month, day, date.getHours(),
date.getMinutes(), date.getSeconds() );
return this;
},
setFullDate: function( year, month, day ) {
this.dateObject = new Date( year, month, day );
return this;
},
adjust: function( period, offset ) {
var date = this.dateObject,
day = period == "D" ? date.getDate() + offset : date.getDate(),
month = period == "M" ? date.getMonth() + offset : date.getMonth(),
year = period == "Y" ? date.getFullYear() + offset : date.getFullYear();
// If not day, update the day to the new month and year
if ( period != "D" ) {
day = Math.max( 1, Math.min( day, this.daysInMonth( year, month ) ) );
}
this.dateObject = new Date( year, month, day, date.getHours(),
date.getMinutes(), date.getSeconds() );
return this;
},
daysInMonth: function( year, month ) {
var date = this.dateObject;
year = year || date.getFullYear();
month = month || date.getMonth();
return 32 - new Date( year, month, 32 ).getDate();
},
monthName: function() {
return Globalize.format( this.dateObject, { pattern: "MMMM" } );
},
day: function() {
return this.dateObject.getDate();
},
month: function() {
return this.dateObject.getMonth();
},
year: function() {
return this.dateObject.getFullYear();
},
isLeapYear: function( year ) {
year = year || this.dateObject.getFullYear();
return new Date( year, 1, 29 ).getMonth() == 1;
},
weekdays: function() {
var result = [];
for ( var dow = 0; dow < 7; dow++ ) {
var day = ( dow + weekdaysRev[ Globalize.locale().supplemental.weekData.firstDay() ] ) % 7;
result.push({
shortname: Globalize.locale().main([ "dates/calendars/gregorian/days/format/abbreviated", weekdays[ day ] ]),
fullname: Globalize.locale().main([ "dates/calendars/gregorian/days/format/wide", weekdays[ day ] ]),
});
}
return result;
},
days: function() {
var result = [],
today = $.date(),
date = this.dateObject,
firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
leadDays = ( firstDayOfMonth - weekdaysRev[ Globalize.locale().supplemental.weekData.firstDay() ] + 7 ) % 7,
rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7 ),
printDate = new Date( this.year(), date.getMonth(), 1 - leadDays );
for ( var row = 0; row < rows; row++ ) {
var week = result[ result.length ] = {
number: Globalize.format( printDate, { pattern: "w" } ),
days: []
};
for ( var dayx = 0; dayx < 7; dayx++ ) {
var day = week.days[ week.days.length ] = {
lead: printDate.getMonth() != date.getMonth(),
date: printDate.getDate(),
timestamp: printDate.getTime(),
current: this.selected && this.selected.equal( printDate ),
today: today.equal( printDate )
};
day.render = day.selectable = !day.lead;
if ( this.eachDay ) {
this.eachDay( day );
}
// TODO use adjust("D", 1)?
printDate.setDate( printDate.getDate() + 1 );
}
}
return result;
},
// specialized for multi-month template, could be used in general
months: function( add ) {
var clone,
result = [ this ];
for ( var i = 0; i < add; i++ ) {
clone = this.clone();
clone.adjust( "M", i + 1 );
result.push( clone );
}
result[ 0 ].first = true;
result[ result.length - 1 ].last = true;
return result;
},
select: function() {
this.selected = this.clone();
return this;
},
clone: function() {
var date = this.dateObject;
return new $.date( new Date( date.getFullYear(), date.getMonth(),
date.getDate(), date.getHours(),
date.getMinutes(), date.getSeconds()), this.globalFormat );
},
// TODO compare year, month, day each for better performance
equal: function( other ) {
function format( date ) {
return Globalize.format( date, { pattern: "yyyyMMdd" } );
}
return format( this.dateObject ) === format( other );
},
date: function() {
return this.dateObject;
},
format: function( format ) {
return Globalize.format( this.dateObject, format || this.globalFormat );
}
};
}( jQuery ));
|