Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 0d64aad

Browse files
committed
feat(datepicker): preserve timezone with model
- Default to using the date instance for any date manipulation, allowing for the timezone to automatically be preserved Closes #4676
1 parent d3056c7 commit 0d64aad

File tree

3 files changed

+19
-47
lines changed

3 files changed

+19
-47
lines changed

src/dateparser/dateparser.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,16 @@ angular.module('ui.bootstrap.dateparser', [])
183183
}
184184

185185
if (isValid(fields.year, fields.month, fields.date)) {
186-
dt = new Date(fields.year, fields.month, fields.date,
187-
fields.hours, fields.minutes, fields.seconds,
188-
fields.milliseconds || 0);
186+
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
187+
dt = new Date(baseDate);
188+
dt.setFullYear(fields.year, fields.month, fields.date,
189+
fields.hours, fields.minutes, fields.seconds,
190+
fields.milliseconds || 0);
191+
} else {
192+
dt = new Date(fields.year, fields.month, fields.date,
193+
fields.hours, fields.minutes, fields.seconds,
194+
fields.milliseconds || 0);
195+
}
189196
}
190197

191198
return dt;

src/datepicker/datepicker.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
143143
return arrays;
144144
};
145145

146-
// Fix a hard-reprodusible bug with timezones
147-
// The bug depends on OS, browser, current timezone and current date
148-
// i.e.
149-
// var date = new Date(2014, 0, 1);
150-
// console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours());
151-
// can result in "2013 11 31 23" because of the bug.
152-
this.fixTimeZone = function(date) {
153-
var hours = date.getHours();
154-
date.setHours(hours === 23 ? hours + 2 : 0);
155-
};
156-
157146
$scope.select = function(date) {
158147
if ($scope.datepickerMode === self.minMode) {
159148
var dt = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : new Date(0, 0, 0, 0, 0, 0, 0);
@@ -238,7 +227,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
238227
var dates = new Array(n), current = new Date(startDate), i = 0, date;
239228
while (i < n) {
240229
date = new Date(current);
241-
this.fixTimeZone(date);
242230
dates[i++] = date;
243231
current.setDate(current.getDate() + 1);
244232
}
@@ -248,8 +236,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
248236
this._refreshView = function() {
249237
var year = this.activeDate.getFullYear(),
250238
month = this.activeDate.getMonth(),
251-
firstDayOfMonth = new Date(year, month, 1),
252-
difference = this.startingDay - firstDayOfMonth.getDay(),
239+
firstDayOfMonth = new Date(this.activeDate);
240+
241+
firstDayOfMonth.setFullYear(year, month, 1);
242+
243+
var difference = this.startingDay - firstDayOfMonth.getDay(),
253244
numDisplayedFromPreviousMonth = (difference > 0) ? 7 - difference : - difference,
254245
firstDate = new Date(firstDayOfMonth);
255246

@@ -340,8 +331,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
340331
date;
341332

342333
for (var i = 0; i < 12; i++) {
343-
date = new Date(year, i, 1);
344-
this.fixTimeZone(date);
334+
date = new Date(this.activeDate);
335+
date.setFullYear(year, i, 1);
345336
months[i] = angular.extend(this.createDateObject(date, this.formatMonth), {
346337
uid: scope.uniqueId + '-' + i
347338
});
@@ -395,8 +386,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
395386
var years = new Array(range), date;
396387

397388
for (var i = 0, start = getStartingYear(this.activeDate.getFullYear()); i < range; i++) {
398-
date = new Date(start + i, 0, 1);
399-
this.fixTimeZone(date);
389+
date = new Date(this.activeDate);
390+
date.setFullYear(start + i, 0, 1);
400391
years[i] = angular.extend(this.createDateObject(date, this.formatYear), {
401392
uid: scope.uniqueId + '-' + i
402393
});

src/datepicker/test/datepicker.spec.js

-26
Original file line numberDiff line numberDiff line change
@@ -397,32 +397,6 @@ describe('datepicker directive', function() {
397397
expect(element.html()).toBe('baz');
398398
});
399399

400-
// issue #3079
401-
describe('time zone bug', function() {
402-
it('should deal with time zone bug', function() {
403-
var ctrl = element.controller('uib-datepicker'),
404-
date = new Date('January 1, 2014');
405-
spyOn(date, 'getHours').and.returnValue(23);
406-
spyOn(date, 'setHours').and.returnValue();
407-
408-
ctrl.fixTimeZone(date);
409-
410-
expect(date.setHours).toHaveBeenCalledWith(25);
411-
});
412-
413-
it('should not change hours if time zone bug does not occur', function() {
414-
var ctrl = element.controller('uib-datepicker'),
415-
date = new Date('January 1, 2014');
416-
spyOn(date, 'getHours').and.returnValue(0);
417-
spyOn(date, 'setHours').and.returnValue();
418-
419-
ctrl.fixTimeZone(date);
420-
421-
expect(date.setHours).toHaveBeenCalledWith(0);
422-
});
423-
424-
});
425-
426400
describe('when `model` changes', function() {
427401
function testCalendar() {
428402
expect(getTitle()).toBe('November 2005');

0 commit comments

Comments
 (0)