diff --git a/src/components/datepicker/js/datepickerDirective.js b/src/components/datepicker/js/datepickerDirective.js
index cf4903c14ac..7eca3b1e456 100644
--- a/src/components/datepicker/js/datepickerDirective.js
+++ b/src/components/datepicker/js/datepickerDirective.js
@@ -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');
@@ -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);
diff --git a/src/components/datepicker/js/datepickerDirective.spec.js b/src/components/datepicker/js/datepickerDirective.spec.js
index 3f3466aa9d0..57881f18807 100644
--- a/src/components/datepicker/js/datepickerDirective.spec.js
+++ b/src/components/datepicker/js/datepickerDirective.spec.js
@@ -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(
+ '');
+ });
+
it('should pass the timezone to the formatting function', function() {
spyOn(controller.locale, 'formatDate');
+ pageScope.modelOptions = {timezone: 'UTC'};
- createDatepickerInstance('');
+ createDatepickerInstance(
+ '');
- 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() {