Skip to content

Commit 988facf

Browse files
Fix midnight wrapping bug and add LOCALE information
1 parent e037fd2 commit 988facf

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

js/datepair.js

+46-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
/*jshint jquery:true */
2+
/*global LOCALE_CODE */
3+
4+
if (typeof LOCALE_CODE != 'undefined') {
5+
var LOCALE_CODE = 'en';
6+
}
27

38
/** Utility for correlated start / end date time selectors.
49
*
@@ -7,7 +12,7 @@
712
* - http://jonthornton.github.io/jquery-timepicker/
813
*/
914

10-
var DatePair = function (shouldSetToNow, dateTimeObjects) {
15+
var DatePair = function (shouldSetToNow, dateTimeObjects, highlightAdvance) {
1116

1217
/** DOM state used by DatePair.
1318
*
@@ -52,6 +57,14 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
5257
this.verifyStartEnd();
5358
};
5459

60+
/* Translate a date into a javascript time object
61+
Assumes that time is in US format '11/13/2013'
62+
*/
63+
this.dateToTime = function (value) {
64+
var splitval = value.split(/[/-]/);
65+
return new Date(splitval[2], splitval[0] - 1, splitval[1]);
66+
};
67+
5568
/** Set entry time to now with a duration of one hour.
5669
*/
5770
this.setToNow = function () {
@@ -72,24 +85,28 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
7285
// Update widget state
7386
this.setStartDateTime(now);
7487
this.setEndDateTime(inOneHour);
88+
this.updateEndTime(now - inOneHour);
7589
};
7690

7791
/** Utility configuring widgets associated with DOM elements.
7892
*/
7993
this.hookUpWidgets = function () {
8094
var thisDatePair = this;
8195

82-
// Starti date and time
96+
// Start date and time
8397
this.startDate.datepicker({
8498
'autoclose': true,
85-
'todayBtn': true,
8699
'todayHighlight': true,
87-
'startDate': '+0d',
88-
'endDate': '+2m'
100+
'startDate': new Date(),
101+
'endDate': '+2m',
102+
'language': LOCALE_CODE
89103
}).change(function () {
90104
thisDatePair.updateEndTime(thisDatePair.priorTimeDelta);
91105
thisDatePair.verifyStartEnd(); // Update text box
92-
thisDatePair.startTime[0].focus(); // Highlight time stamp
106+
107+
if (highlightAdvance) {
108+
thisDatePair.startTime[0].focus(); // Highlight time stamp
109+
}
93110
});
94111
this.startTime.timepicker({
95112
'timeFormat': 'g:ia',
@@ -102,19 +119,29 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
102119
// End date and time
103120
this.endDate.datepicker({
104121
'autoclose': true,
105-
'startDate': '+0d',
106-
'endDate': '+2m'
122+
'startDate': new Date(),
123+
'endDate': '+2m',
124+
'language': LOCALE_CODE
107125
}).change(function () {
108126
thisDatePair.verifyStartEnd(); // Update text box
109127
thisDatePair.priorTimeDelta = thisDatePair.getTimeDelta();
110128
thisDatePair.updateEndTime(thisDatePair.priorTimeDelta);
111-
thisDatePair.endTime[0].focus(); // Highlight time stamp
129+
130+
if (highlightAdvance) {
131+
thisDatePair.endTime[0].focus(); // Highlight time stamp
132+
}
112133
}).data('datepicker');
113134
this.endTime.timepicker({
114135
'timeFormat': 'g:ia',
115136
'scrollDefaultNow': true
116137
}).change(function () {
117-
thisDatePair.priorTimeDelta = thisDatePair.getTimeDelta();
138+
// if we accidentally loop around midnight...
139+
// then subtract 24 hours (86400 seconds) from the delta
140+
if (thisDatePair.priorTimeDelta <= 86400 && thisDatePair.getTimeDelta() >= 86400) {
141+
thisDatePair.priorTimeDelta = thisDatePair.getTimeDelta() - 86400;
142+
} else {
143+
thisDatePair.priorTimeDelta = thisDatePair.getTimeDelta();
144+
}
118145
thisDatePair.updateEndTime(thisDatePair.priorTimeDelta);
119146
thisDatePair.verifyStartEnd(); // Update text box
120147
});
@@ -144,7 +171,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
144171
if (timeDelta < (this.oneHourMS * 24 / 1000)) {
145172
startSeconds = this.startTime.timepicker('getSecondsFromMidnight');
146173
this.endTime.timepicker('option', 'minTime', startSeconds + 30 * 60);
147-
this.endTime.timepicker('option', 'maxTime', '12:00pm');
174+
this.endTime.timepicker('option', 'maxTime', '11:30pm');
148175
this.endTime.timepicker('option', 'durationTime', startSeconds);
149176
this.endTime.timepicker('option', 'showDuration', true);
150177
} else {
@@ -174,7 +201,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
174201
var date,
175202
startDate,
176203
startSeconds;
177-
startDate = new Date(this.startDate.val());
204+
startDate = this.dateToTime(this.startDate.val());
178205
startSeconds = this.startTime.timepicker('getSecondsFromMidnight');
179206
date = new Date(startDate.getTime() + startSeconds * 1000);
180207
return date;
@@ -188,7 +215,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
188215
var date,
189216
endDate,
190217
endSeconds;
191-
endDate = new Date(this.endDate.val());
218+
endDate = this.dateToTime(this.endDate.val());
192219
endSeconds = this.endTime.timepicker('getSecondsFromMidnight');
193220
date = new Date(endDate.getTime() + endSeconds * 1000);
194221
return date;
@@ -217,13 +244,8 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
217244
* param {Date} End Date object.
218245
*/
219246
this.setEndDateTime = function (date) {
220-
var endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
221-
222-
//if end time is 11:30pm, change end date to start date
223-
if((date.getHours() + ":" + date.getMinutes()) == "23:30"
224-
&& this.getTimeDelta() < (this.oneHourMS * 24)) {
225-
endDate.setDate(this.getStartDateTime().getDate());
226-
}
247+
var startDate = this.getStartDateTime(),
248+
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
227249

228250
// Check for NaN.. should be logging an error here
229251
if (isNaN(endDate)) {
@@ -284,8 +306,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
284306
var configurations,
285307
timeCookie;
286308
configurations = {
287-
path: '/', // set globally for the site
288-
expires: 1 // one hour expiration
309+
path: '/' // set globally for the site
289310
};
290311
timeCookie = {
291312
startDateTime: this.getStartDateTime(),
@@ -313,9 +334,9 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
313334
hourString = (hourString === '0 hours') ? '': hourString;
314335
dateTimeDeltaMinutes -= (hourInt * 60);
315336

316-
minuteString = dateTimeDeltaMinutes + ' minutes';
317-
minuteString = (minuteString === '1 minutes') ? '1 minute': minuteString;
318-
minuteString = (minuteString === '0 minutes') ? '': minuteString;
337+
minuteString = dateTimeDeltaMinutes + ' mins';
338+
minuteString = (minuteString === '1 min') ? '1 min': minuteString;
339+
minuteString = (minuteString === '0 mins') ? '': minuteString;
319340

320341
$('.output-duration').text(dateString + ' ' + hourString + ' ' + minuteString);
321342
};

0 commit comments

Comments
 (0)