-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.date.js
executable file
·135 lines (131 loc) · 3.84 KB
/
jquery.date.js
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
/*
* jQuery Date
*
* Copyright 2010 Marc Grabanski
* Licensed under the MIT license
*
*
* Depends:
* jquery.glob.js
*/
(function( $, undefined ) {
if ( typeof( $.global.culture ) == "undefined" ) {
$.global.culture = $.global.cultures[ "default" ];
}
$.date = function ( datestring, formatstring ) {
var calendar = $.global.culture.calendar,
format = formatstring ? formatstring : calendar.patterns.d,
date = datestring ? $.global.parseDate(datestring, format) : new Date();
return {
refresh: function() {
calendar = $.global.culture.calendar;
format = formatstring || calendar.patterns.d;
return this;
},
setFormat: function( formatstring ) {
if ( formatstring ) {
format = formatstring;
}
return this;
},
setDay: function( day ) {
date = new Date( date.getFullYear(), date.getMonth(), day );
return this;
},
adjust: function( period, offset ) {
var day = period == "D" ? date.getDate() + offset : date.getDate(),
month = period == "M" ? date.getMonth() + offset : date.getMonth(),
year = period == "Y" ? date.getFullYear() + offset : date.getFullYear();
date = new Date( year, month, day );
return this;
},
daysInMonth: function( year, month ) {
year = year || date.getFullYear();
month = month || date.getMonth();
return 32 - new Date( year, month, 32 ).getDate();
},
monthname: function() {
return calendar.months.names[ date.getMonth() ];
},
year: function() {
return date.getFullYear();
},
weekdays: function() {
// TODO take firstDay into account
var result = [];
for ( var dow = 0; dow < 7; dow++ ) {
var day = ( dow + calendar.firstDay ) % 7;
result.push( {
shortname: calendar.days.namesShort[ day ],
fullname: calendar.days.names[ day ],
});
}
return result;
},
days: function() {
var result = [],
firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
leadDays = ( firstDayOfMonth - calendar.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: this.iso8601Week( printDate ),
days: []
};
for ( var dayx = 0; dayx < 7; dayx++ ) {
var day = week.days[ week.days.length ] = {
lead: printDate.getMonth() != date.getMonth(),
date: printDate.getDate(),
current: this.selected && this.selected.equal( printDate ),
today: today.equal( printDate )
};
day.render = day.selectable = !day.lead;
this.eachDay( day );
// TODO use adjust("D", 1)?
printDate.setDate( printDate.getDate() + 1 );
}
}
return result;
},
iso8601Week: function( date ) {
var checkDate = new Date( date.getTime() );
// Find Thursday of this week starting on Monday
checkDate.setDate( checkDate.getDate() + 4 - ( checkDate.getDay() || 7 ) );
var time = checkDate.getTime();
checkDate.setMonth( 0 ); // Compare with Jan 1
checkDate.setDate( 1 );
return Math.floor( Math.round( ( time - checkDate ) / 86400000) / 7 ) + 1;
},
select: function() {
this.selected = this.clone();
return this;
},
// TODO create new Date with year, month, day instead
clone: function() {
return $.date( this.format(), format );
},
// TODO compare year, month, day each for better performance
equal: function( other ) {
function format( date ) {
return $.global.format( date, "d" );
}
return format( date ) == format( other );
},
date: function() {
return date;
},
format: function( formatstring ) {
return $.global.format( date, formatstring ? formatstring : format );
},
calendar: function( newcalendar ) {
if ( newcalendar ) {
calendar = newcalendar;
return this;
}
return calendar;
}
}
}
var today = $.date();
}( jQuery ));