Skip to content

Commit 69ab148

Browse files
Sean Cadyjzaefferer
Sean Cady
authored andcommitted
Date: Added Hour, minute, second to date creation; Added logic to handle day overflow on year or month change; Fixed naming of monthname. Added tests for everything. Finish merge from master.
1 parent 739a21e commit 69ab148

File tree

5 files changed

+341
-33
lines changed

5 files changed

+341
-33
lines changed

datepicker-rewrite/date.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
*/
77
(function( $, undefined ) {
88

9-
$.date = function ( datestring, formatstring ) {
9+
$.date = function( datestring, formatstring ) {
1010
//TODO: Need to refactor $.date to be a constructor, move the methods to a prototype.
1111
var calendar = Globalize.culture().calendar,
1212
format = formatstring ? formatstring : calendar.patterns.d,
13-
date = datestring ? Globalize.parseDate(datestring, format) : new Date();
13+
date = datestring ? Globalize.parseDate( datestring, format ) : new Date();
1414

1515
if ( !date ) {
16-
date = new Date()
16+
date = new Date();
1717
}
1818

1919
return {
@@ -35,18 +35,30 @@ $.date = function ( datestring, formatstring ) {
3535
return this;
3636
},
3737
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() );
3939
return this;
4040
},
4141
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() );
4551
return this;
4652
},
4753
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() );
5062
return this;
5163
},
5264
setFullDate: function( year, month, day ) {
@@ -57,19 +69,19 @@ $.date = function ( datestring, formatstring ) {
5769
var day = period == "D" ? date.getDate() + offset : date.getDate(),
5870
month = period == "M" ? date.getMonth() + offset : date.getMonth(),
5971
year = period == "Y" ? date.getFullYear() + offset : date.getFullYear();
60-
72+
// If not day, update the day to the new month and year
6173
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 ) ) );
6375
}
64-
date = new Date( year, month, day );
76+
date = new Date( year, month, day, date.getHours(), date.getMinutes(), date.getSeconds() );
6577
return this;
6678
},
6779
daysInMonth: function( year, month ) {
6880
year = year || date.getFullYear();
6981
month = month || date.getMonth();
7082
return 32 - new Date( year, month, 32 ).getDate();
7183
},
72-
monthname: function() {
84+
monthName: function() {
7385
return calendar.months.names[ date.getMonth() ];
7486
},
7587
day: function() {
@@ -81,12 +93,16 @@ $.date = function ( datestring, formatstring ) {
8193
year: function() {
8294
return date.getFullYear();
8395
},
96+
isLeapYear: function( year ) {
97+
year = year || date.getFullYear();
98+
return new Date( year, 1, 29 ).getMonth() == 1;
99+
100+
},
84101
weekdays: function() {
85-
// TODO take firstDay into account
86102
var result = [];
87103
for ( var dow = 0; dow < 7; dow++ ) {
88104
var day = ( dow + calendar.firstDay ) % 7;
89-
result.push( {
105+
result.push({
90106
shortname: calendar.days.namesShort[ day ],
91107
fullname: calendar.days.names[ day ]
92108
});
@@ -98,7 +114,7 @@ $.date = function ( datestring, formatstring ) {
98114
today = $.date(),
99115
firstDayOfMonth = new Date( this.year(), date.getMonth(), 1 ).getDay(),
100116
leadDays = ( firstDayOfMonth - calendar.firstDay + 7 ) % 7,
101-
rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7),
117+
rows = Math.ceil( ( leadDays + this.daysInMonth() ) / 7 ),
102118
printDate = new Date( this.year(), date.getMonth(), 1 - leadDays );
103119
for ( var row = 0; row < rows; row++ ) {
104120
var week = result[ result.length ] = {
@@ -114,52 +130,52 @@ $.date = function ( datestring, formatstring ) {
114130
today: today.equal( printDate )
115131
};
116132
day.render = day.selectable = !day.lead;
117-
this.eachDay( day );
133+
// TODO undefined in picker demos, fix it
134+
// this.eachDay( day );
118135
// TODO use adjust("D", 1)?
119136
printDate.setDate( printDate.getDate() + 1 );
120137
}
121138
}
122139
return result;
123140
},
124-
// specialzed for multi-month template, could be used in general
141+
// specialized for multi-month template, could be used in general
125142
months: function( add ) {
126143
var result = [],
127-
current = date.getMonth(),
128-
self = this;
144+
current = date.getMonth();
129145
for ( var i = 0; i < add + 1; i++ ) {
130146
result.push( this.clone() );
131147
this.adjust( "M", 1 );
132148
}
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 );
136152
return result;
137153
},
138-
iso8601Week: function( date ) {
154+
iso8601Week: function(date) {
139155
var checkDate = new Date( date.getTime() );
140156
// Find Thursday of this week starting on Monday
141157
checkDate.setDate( checkDate.getDate() + 4 - ( checkDate.getDay() || 7 ) );
142158
var time = checkDate.getTime();
143-
checkDate.setMonth( 0 ); // Compare with Jan 1
159+
// Compare with Jan 1
160+
checkDate.setMonth( 0 );
144161
checkDate.setDate( 1 );
145162
return Math.floor( Math.round( ( time - checkDate ) / 86400000) / 7 ) + 1;
146163
},
147164
select: function() {
148165
this.selected = this.clone();
149166
return this;
150167
},
151-
// TODO create new Date with year, month, day instead
152168
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 );
156172
},
157173
// TODO compare year, month, day each for better performance
158174
equal: function( other ) {
159175
function format( date ) {
160176
return Globalize.format( date, "d" );
161177
}
162-
return format( date ) == format( other );
178+
return format( date ) === format( other );
163179
},
164180
date: function() {
165181
return date;
@@ -174,7 +190,7 @@ $.date = function ( datestring, formatstring ) {
174190
}
175191
return calendar;
176192
}
177-
}
178-
}
193+
};
194+
};
179195

180196
}( jQuery ));

datepicker-rewrite/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<script src="../jquery-1.7.1.js"></script>
5+
<script src="../jquery-1.8.2.js"></script>
66
<script src="../ui/jquery.ui.core.js"></script>
77
<script src="../ui/jquery.ui.widget.js"></script>
88
<script src="../ui/jquery.ui.button.js"></script>

tests/unit/date/all.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jQuery UI Date Test Suite</title>
6+
7+
<script src="../../../jquery-1.7.1.js"></script>
8+
9+
<link rel="stylesheet" href="../../../external/qunit.css">
10+
<link rel="stylesheet" href="../subsuiteRunner.css">
11+
<script src="../../../external/qunit.js"></script>
12+
<script src="../subsuiteRunner.js"></script>
13+
<script src="../subsuite.js"></script>
14+
15+
<script>
16+
testAllVersions( "date" );
17+
</script>
18+
</head>
19+
<body>
20+
21+
<h1 id="qunit-header">jQuery UI Datepicker Test Suite</h1>
22+
<h2 id="qunit-banner"></h2>
23+
<div id="qunit-testrunner-toolbar"></div>
24+
<h2 id="qunit-userAgent"></h2>
25+
<ol id="qunit-tests"></ol>
26+
<div id="qunit-fixture">
27+
28+
</div>
29+
</body>
30+
</html>

tests/unit/date/date.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jQuery UI Date Test Suite</title>
6+
7+
<script src="../../jquery.js"></script>
8+
<link rel="stylesheet" href="../../../external/qunit.css">
9+
<script src="../../../external/qunit.js"></script>
10+
<script src="../../jquery.simulate.js"></script>
11+
<script src="../testsuite.js"></script>
12+
<script>
13+
TestHelpers.loadResources({
14+
js: [
15+
"datepicker-rewrite/date.js",
16+
"external/globalize.js",
17+
"external/globalize.culture.de-DE.js"
18+
]
19+
});
20+
</script>
21+
22+
<script src="date_core.js"></script>
23+
24+
<script src="../swarminject.js"></script>
25+
</head>
26+
<body>
27+
28+
<h1 id="qunit-header">jQuery UI Date Test Suite</h1>
29+
<h2 id="qunit-banner"></h2>
30+
<div id="qunit-testrunner-toolbar"></div>
31+
<h2 id="qunit-userAgent"></h2>
32+
<ol id="qunit-tests"></ol>
33+
<div id="qunit-fixture">
34+
35+
<div><input type="text" id="inp"><input type="text" id="alt"><div id="inl"></div></div>
36+
<p><input type="text" id="inp2"></p>
37+
38+
</div>
39+
</body>
40+
</html>

0 commit comments

Comments
 (0)