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

Commit 7856883

Browse files
committed
fix(datepicker): null model with timezone throws exception
- verify that the model value is not null or undefined before accessing `.getTimezoneOffset()` Fixes #12025
1 parent 19c6c75 commit 7856883

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/components/datepicker/js/datepickerDirective.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@
992992
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
993993
// Using the timezone when the offset is negative (GMT+X) causes the previous day to be
994994
// set as the model value here. This check avoids that.
995-
if (timezone == null || value.getTimezoneOffset() < 0) {
995+
if (timezone == null || value == null || value.getTimezoneOffset() < 0) {
996996
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd'), 'default');
997997
} else {
998998
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd', timezone), 'default');
@@ -1014,7 +1014,7 @@
10141014
}
10151015
// Using the timezone when the offset is negative (GMT+X) causes the previous day to be
10161016
// used here. This check avoids that.
1017-
if (timezone == null || value.getTimezoneOffset() < 0) {
1017+
if (timezone == null || value == null || value.getTimezoneOffset() < 0) {
10181018
this.inputElement.value = this.locale.formatDate(value);
10191019
} else {
10201020
this.inputElement.value = this.locale.formatDate(value, timezone);

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,21 @@ describe('md-datepicker', function() {
139139
expect(ngElement.attr('type')).toBe('date');
140140
});
141141

142+
it('should handle an initial ng-model that is null when timezone is specified', function() {
143+
pageScope.modelOptions = {timezone: 'UTC'};
144+
pageScope.myDate = null;
145+
createDatepickerInstance(
146+
'<md-datepicker ng-model="myDate" ng-model-options="modelOptions"></md-datepicker>');
147+
});
148+
142149
it('should pass the timezone to the formatting function', function() {
143150
spyOn(controller.locale, 'formatDate');
151+
pageScope.modelOptions = {timezone: 'UTC'};
144152

145-
createDatepickerInstance('<md-datepicker ng-model="myDate" ' +
146-
'ng-model-options="{ timezone: \'utc\' }"></md-datepicker>');
153+
createDatepickerInstance(
154+
'<md-datepicker ng-model="myDate" ng-model-options="modelOptions"></md-datepicker>');
147155

148-
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'utc');
156+
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'UTC');
149157
});
150158

151159
it('should allow for the locale to be overwritten on a specific element', function() {

0 commit comments

Comments
 (0)