Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 22f9faf

Browse files
crisbetojelbourn
authored andcommitted
fix(datepicker): pass in the timezone when formatting the date (#9837)
* Passes in the timezone when formatting the date in the datepicker directive. * Combines a few redundant calls into a method. Fixes #9725.
1 parent bd605c0 commit 22f9faf

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/components/datepicker/js/dateLocaleProvider.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
* @property {(Array<string>)=} firstDayOfWeek The first day of the week. Sunday = 0, Monday = 1,
2222
* etc.
2323
* @property {(function(string): Date)=} parseDate Function to parse a date object from a string.
24-
* @property {(function(Date): string)=} formatDate Function to format a date object to a string.
24+
* @property {(function(Date, string): string)=} formatDate Function to format a date object to a
25+
* string. The datepicker directive also provides the time zone, if it was specified.
2526
* @property {(function(Date): string)=} monthHeaderFormatter Function that returns the label for
2627
* a month given a date.
2728
* @property {(function(Date): string)=} monthFormatter Function that returns the full name of a month
@@ -161,9 +162,10 @@
161162
/**
162163
* Default date-to-string formatting function.
163164
* @param {!Date} date
165+
* @param {string=} timezone
164166
* @returns {string}
165167
*/
166-
function defaultFormatDate(date) {
168+
function defaultFormatDate(date, timezone) {
167169
if (!date) {
168170
return '';
169171
}
@@ -180,7 +182,7 @@
180182
formatDate = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 1, 0, 0);
181183
}
182184

183-
return $filter('date')(formatDate, 'M/d/yyyy');
185+
return $filter('date')(formatDate, 'M/d/yyyy', timezone);
184186
}
185187

186188
/**

src/components/datepicker/js/datepickerDirective.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,7 @@
409409
'Currently the model is a: ' + (typeof value));
410410
}
411411

412-
self.date = value;
413-
self.inputElement.value = self.dateLocale.formatDate(value);
414-
self.mdInputContainer && self.mdInputContainer.setHasValue(!!value);
415-
self.resizeInputElement();
416-
self.updateErrorState();
412+
self.onExternalChange(value);
417413

418414
return value;
419415
});
@@ -444,12 +440,8 @@
444440

445441
self.$scope.$on('md-calendar-change', function(event, date) {
446442
self.setModelValue(date);
447-
self.date = date;
448-
self.inputElement.value = self.dateLocale.formatDate(date);
449-
self.mdInputContainer && self.mdInputContainer.setHasValue(!!date);
443+
self.onExternalChange(date);
450444
self.closeCalendarPane();
451-
self.resizeInputElement();
452-
self.updateErrorState();
453445
});
454446

455447
self.ngInputElement.on('input', angular.bind(self, self.resizeInputElement));
@@ -860,6 +852,22 @@
860852
* @param {Date=} value Date to be set as the model value.
861853
*/
862854
DatePickerCtrl.prototype.setModelValue = function(value) {
863-
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd'));
855+
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
856+
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd', timezone));
857+
};
858+
859+
/**
860+
* Updates the datepicker when a model change occurred externally.
861+
* @param {Date=} value Value that was set to the model.
862+
*/
863+
DatePickerCtrl.prototype.onExternalChange = function(value) {
864+
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
865+
866+
this.date = value;
867+
this.inputElement.value = this.dateLocale.formatDate(value, timezone);
868+
this.mdInputContainer && this.mdInputContainer.setHasValue(!!value);
869+
this.closeCalendarPane();
870+
this.resizeInputElement();
871+
this.updateErrorState();
864872
};
865873
})();

src/components/datepicker/js/datepickerDirective.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ describe('md-datepicker', function() {
128128
expect(ngElement.attr('type')).toBe('date');
129129
});
130130

131+
it('should pass the timezone to the formatting function', function() {
132+
spyOn(dateLocale, 'formatDate');
133+
134+
createDatepickerInstance('<md-datepicker ng-model="myDate" ' +
135+
'ng-model-options="{ timezone: \'utc\' }"></md-datepicker>');
136+
137+
expect(dateLocale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'utc');
138+
});
139+
131140
describe('ngMessages support', function() {
132141
it('should set the `required` $error flag', function() {
133142
pageScope.isRequired = true;

0 commit comments

Comments
 (0)