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

fix(datepicker): issues in UTC+x time zones when ng-model-options="{timezone: 'UTC'}" is set #12150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<md-content ng-controller="AppCtrl as ctrl" layout="row" layout-padding ng-cloak>
<md-calendar ng-model="ctrl.calendarDate" ng-model-options="{timezone: 'UTC'}">
</md-calendar>

<div layout="column" layout-padding>
<div>
<h4>Calendar Values</h4>
<div>
<strong>Date in local timezone:</strong>
{{ctrl.calendarDate|date:"yyyy-MM-dd HH:mm Z"}}
</div>
<div>
<strong>Date in UTC timezone:</strong>
{{ctrl.calendarDate|date:"yyyy-MM-dd HH:mm Z":"UTC"}}
</div>
</div>
<md-divider></md-divider>
<md-datepicker ng-model="ctrl.datepickerDate" ng-model-options="{timezone: 'UTC'}">
</md-datepicker>
<div>
<h4>Datepicker Values</h4>
<div>
<strong>Date in local timezone:</strong>
{{ctrl.datepickerDate|date:"yyyy-MM-dd HH:mm Z"}}
</div>
<div>
<strong>Date in UTC timezone:</strong>
{{ctrl.datepickerDate|date:"yyyy-MM-dd HH:mm Z":"UTC"}}
</div>
</div>
</div>
</md-content>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
angular.module('ngModelTimezoneUsage', ['ngMaterial', 'ngMessages'])
.controller('AppCtrl', function() {
this.datepickerDate = new Date(0);
this.datepickerDate.setUTCFullYear(2020, 5, 19);

// represents isssue: https://github.com/angular/material/issues/12149 , when input box initial values do not takes into account passed timezone option.
this.datepickerDate.setUTCHours(23, 0, 0, 0);

this.calendarDate = new Date(0);
this.calendarDate.setUTCFullYear(2020, 5, 19);

// represents isssue: https://github.com/angular/material/issues/12149 , when input box initial values do not takes into account passed timezone option.
this.datepickerDate.setUTCHours(23, 0, 0, 0);
});
18 changes: 15 additions & 3 deletions src/components/datepicker/js/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,23 @@
CalendarCtrl.prototype.setNgModelValue = function(date) {
var timezone = this.$mdUtil.getModelOption(this.ngModelCtrl, 'timezone');
var value = this.dateUtil.createDateAtMidnight(date);

if (timezone != null && timezone != "") {
// If timezone is specified - then convert to midnight of specified timezone to display
var offset = value.getTimezoneOffset();
if (offset < 0) {
// Convert negative offset to positive
value.setMinutes(-1*(offset), 0, 0);
}
else {
value.setMinutes(offset, 0, 0);
}
}

this.focusDate(value);
this.$scope.$emit('md-calendar-change', value);
// Using the timezone when the offset is negative (GMT+X) causes the previous day to be
// selected here. This check avoids that.
if (timezone == null || value.getTimezoneOffset() < 0) {

if (timezone == null || timezone == "") {
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd'), 'default');
} else {
this.ngModelCtrl.$setViewValue(this.ngDateFilter(value, 'yyyy-MM-dd', timezone), 'default');
Expand Down