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

fix(datepicker): null model with timezone throws exception #12027

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/datepicker/js/datepickerDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
// Using the timezone when the offset is negative (GMT+X) causes the previous day to be
// set as the model value here. This check avoids that.
if (timezone == null || value.getTimezoneOffset() < 0) {
if (timezone == null || value == null || value.getTimezoneOffset() < 0) {
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd'), 'default');
} else {
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd', timezone), 'default');
Expand All @@ -1014,7 +1014,7 @@
}
// Using the timezone when the offset is negative (GMT+X) causes the previous day to be
// used here. This check avoids that.
if (timezone == null || value.getTimezoneOffset() < 0) {
if (timezone == null || value == null || value.getTimezoneOffset() < 0) {
this.inputElement.value = this.locale.formatDate(value);
} else {
this.inputElement.value = this.locale.formatDate(value, timezone);
Expand Down
14 changes: 11 additions & 3 deletions src/components/datepicker/js/datepickerDirective.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,21 @@ describe('md-datepicker', function() {
expect(ngElement.attr('type')).toBe('date');
});

it('should handle an initial ng-model that is null when timezone is specified', function() {
pageScope.modelOptions = {timezone: 'UTC'};
pageScope.myDate = null;
createDatepickerInstance(
'<md-datepicker ng-model="myDate" ng-model-options="modelOptions"></md-datepicker>');
});

it('should pass the timezone to the formatting function', function() {
spyOn(controller.locale, 'formatDate');
pageScope.modelOptions = {timezone: 'UTC'};

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

expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'utc');
expect(controller.locale.formatDate).toHaveBeenCalledWith(pageScope.myDate, 'UTC');
});

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