6
6
*/
7
7
( function ( $ , undefined ) {
8
8
9
- $ . date = function ( datestring , formatstring ) {
9
+ $ . date = function ( datestring , formatstring ) {
10
10
//TODO: Need to refactor $.date to be a constructor, move the methods to a prototype.
11
11
var calendar = Globalize . culture ( ) . calendar ,
12
12
format = formatstring ? formatstring : calendar . patterns . d ,
13
- date = datestring ? Globalize . parseDate ( datestring , format ) : new Date ( ) ;
13
+ date = datestring ? Globalize . parseDate ( datestring , format ) : new Date ( ) ;
14
14
15
15
if ( ! date ) {
16
- date = new Date ( )
16
+ date = new Date ( ) ;
17
17
}
18
18
19
19
return {
@@ -35,18 +35,30 @@ $.date = function ( datestring, formatstring ) {
35
35
return this ;
36
36
} ,
37
37
setDay : function ( day ) {
38
- date = new Date ( date . getFullYear ( ) , date . getMonth ( ) , day ) ;
38
+ date = new Date ( date . getFullYear ( ) , date . getMonth ( ) , day , date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ) ;
39
39
return this ;
40
40
} ,
41
41
setMonth : function ( month ) {
42
- //TODO: Should update this to keep the time component (hour, min, sec) intact. Same for setYear and setFullDate.
43
- //TODO: Do we want to do any special handling when switching to a month that causes the days to overflow?
44
- date = new Date ( date . getFullYear ( ) , month , date . getDate ( ) ) ;
42
+ // Overflow example: Month is October 31 (yeah Halloween) and month is changed to April with 30 days,
43
+ // the new date will me May 1. We will honor the month the user wants to set and if and overflow
44
+ // occurs, set to last day of month.
45
+ var days = date . getDay ( ) , year = date . getFullYear ( ) ;
46
+ if ( days > this . daysInMonth ( year , month ) ) {
47
+ // Overflow
48
+ days = this . daysInMonth ( year , month ) ;
49
+ }
50
+ date = new Date ( year , month , days , date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ) ;
45
51
return this ;
46
52
} ,
47
53
setYear : function ( year ) {
48
- //TODO: Same question as setMonth, but I suppose only for leap years.
49
- date = new Date ( year , date . getMonth ( ) , date . getDate ( ) ) ;
54
+ var day = date . getDate ( ) ,
55
+ month = date . getMonth ( ) ;
56
+ // Check if Leap, and February and day is 29th
57
+ if ( this . isLeapYear ( year ) && month == 1 && day == 29 ) {
58
+ // set day to last day of February
59
+ day = this . daysInMonth ( year , month ) ;
60
+ }
61
+ date = new Date ( year , month , day , date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ) ;
50
62
return this ;
51
63
} ,
52
64
setFullDate : function ( year , month , day ) {
@@ -57,19 +69,19 @@ $.date = function ( datestring, formatstring ) {
57
69
var day = period == "D" ? date . getDate ( ) + offset : date . getDate ( ) ,
58
70
month = period == "M" ? date . getMonth ( ) + offset : date . getMonth ( ) ,
59
71
year = period == "Y" ? date . getFullYear ( ) + offset : date . getFullYear ( ) ;
60
-
72
+ // If not day, update the day to the new month and year
61
73
if ( period != "D" ) {
62
- day = Math . max ( 1 , Math . min ( day , this . daysInMonth ( year , month ) ) ) ;
74
+ day = Math . max ( 1 , Math . min ( day , this . daysInMonth ( year , month ) ) ) ;
63
75
}
64
- date = new Date ( year , month , day ) ;
76
+ date = new Date ( year , month , day , date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ) ;
65
77
return this ;
66
78
} ,
67
79
daysInMonth : function ( year , month ) {
68
80
year = year || date . getFullYear ( ) ;
69
81
month = month || date . getMonth ( ) ;
70
82
return 32 - new Date ( year , month , 32 ) . getDate ( ) ;
71
83
} ,
72
- monthname : function ( ) {
84
+ monthName : function ( ) {
73
85
return calendar . months . names [ date . getMonth ( ) ] ;
74
86
} ,
75
87
day : function ( ) {
@@ -81,12 +93,16 @@ $.date = function ( datestring, formatstring ) {
81
93
year : function ( ) {
82
94
return date . getFullYear ( ) ;
83
95
} ,
96
+ isLeapYear : function ( year ) {
97
+ year = year || date . getFullYear ( ) ;
98
+ return new Date ( year , 1 , 29 ) . getMonth ( ) == 1 ;
99
+
100
+ } ,
84
101
weekdays : function ( ) {
85
- // TODO take firstDay into account
86
102
var result = [ ] ;
87
103
for ( var dow = 0 ; dow < 7 ; dow ++ ) {
88
104
var day = ( dow + calendar . firstDay ) % 7 ;
89
- result . push ( {
105
+ result . push ( {
90
106
shortname : calendar . days . namesShort [ day ] ,
91
107
fullname : calendar . days . names [ day ]
92
108
} ) ;
@@ -98,7 +114,7 @@ $.date = function ( datestring, formatstring ) {
98
114
today = $ . date ( ) ,
99
115
firstDayOfMonth = new Date ( this . year ( ) , date . getMonth ( ) , 1 ) . getDay ( ) ,
100
116
leadDays = ( firstDayOfMonth - calendar . firstDay + 7 ) % 7 ,
101
- rows = Math . ceil ( ( leadDays + this . daysInMonth ( ) ) / 7 ) ,
117
+ rows = Math . ceil ( ( leadDays + this . daysInMonth ( ) ) / 7 ) ,
102
118
printDate = new Date ( this . year ( ) , date . getMonth ( ) , 1 - leadDays ) ;
103
119
for ( var row = 0 ; row < rows ; row ++ ) {
104
120
var week = result [ result . length ] = {
@@ -114,52 +130,52 @@ $.date = function ( datestring, formatstring ) {
114
130
today : today . equal ( printDate )
115
131
} ;
116
132
day . render = day . selectable = ! day . lead ;
117
- this . eachDay ( day ) ;
133
+ // TODO undefined in picker demos, fix it
134
+ // this.eachDay( day );
118
135
// TODO use adjust("D", 1)?
119
136
printDate . setDate ( printDate . getDate ( ) + 1 ) ;
120
137
}
121
138
}
122
139
return result ;
123
140
} ,
124
- // specialzed for multi-month template, could be used in general
141
+ // specialized for multi-month template, could be used in general
125
142
months : function ( add ) {
126
143
var result = [ ] ,
127
- current = date . getMonth ( ) ,
128
- self = this ;
144
+ current = date . getMonth ( ) ;
129
145
for ( var i = 0 ; i < add + 1 ; i ++ ) {
130
146
result . push ( this . clone ( ) ) ;
131
147
this . adjust ( "M" , 1 ) ;
132
148
}
133
- result [ 0 ] . first = true ;
134
- result [ result . length - 1 ] . last = true ;
135
- date . setMonth ( current ) ;
149
+ result [ 0 ] . first = true ;
150
+ result [ result . length - 1 ] . last = true ;
151
+ date . setMonth ( current ) ;
136
152
return result ;
137
153
} ,
138
- iso8601Week : function ( date ) {
154
+ iso8601Week : function ( date ) {
139
155
var checkDate = new Date ( date . getTime ( ) ) ;
140
156
// Find Thursday of this week starting on Monday
141
157
checkDate . setDate ( checkDate . getDate ( ) + 4 - ( checkDate . getDay ( ) || 7 ) ) ;
142
158
var time = checkDate . getTime ( ) ;
143
- checkDate . setMonth ( 0 ) ; // Compare with Jan 1
159
+ // Compare with Jan 1
160
+ checkDate . setMonth ( 0 ) ;
144
161
checkDate . setDate ( 1 ) ;
145
162
return Math . floor ( Math . round ( ( time - checkDate ) / 86400000 ) / 7 ) + 1 ;
146
163
} ,
147
164
select : function ( ) {
148
165
this . selected = this . clone ( ) ;
149
166
return this ;
150
167
} ,
151
- // TODO create new Date with year, month, day instead
152
168
clone : function ( ) {
153
- var result = $ . date ( this . format ( ) , format ) ;
154
- result . eachDay = this . eachDay ;
155
- return result ;
169
+ return $ . date ( new Date ( date . getFullYear ( ) , date . getMonth ( ) ,
170
+ date . getDate ( ) , date . getHours ( ) ,
171
+ date . getMinutes ( ) , date . getSeconds ( ) ) , formatstring ) ;
156
172
} ,
157
173
// TODO compare year, month, day each for better performance
158
174
equal : function ( other ) {
159
175
function format ( date ) {
160
176
return Globalize . format ( date , "d" ) ;
161
177
}
162
- return format ( date ) == format ( other ) ;
178
+ return format ( date ) === format ( other ) ;
163
179
} ,
164
180
date : function ( ) {
165
181
return date ;
@@ -174,7 +190,7 @@ $.date = function ( datestring, formatstring ) {
174
190
}
175
191
return calendar ;
176
192
}
177
- }
178
- }
193
+ } ;
194
+ } ;
179
195
180
196
} ( jQuery ) ) ;
0 commit comments