Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Fix for model binding of date that has ng-model-options' timezone #175

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 32 additions & 21 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@ import jQuery from 'jquery';
import angular from 'angular';
import _datePicker from 'jquery-ui/datepicker'; // sets up jQuery with the datepicker plugin

function addDateMinutes(date, minutes) {
date = new Date(date.getTime());
date.setMinutes(date.getMinutes() + minutes);
return date;
}

function convertTimezoneToLocal(date, timezone, reverse) {
if (!date) {
return date;
}

reverse = reverse ? -1 : 1;
var dateTimezoneOffset = date.getTimezoneOffset();
var timezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset);
return addDateMinutes(date, reverse * (timezoneOffset - dateTimezoneOffset));
}

//https://github.com/angular/angular.js/blob/622c42169699ec07fc6daaa19fe6d224e5d2f70e/src/Angular.js#L1207
function timezoneToOffset(timezone, fallback) {
timezone = timezone.replace(/:/g, '');
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
}

export default angular.module('ui.date', [])
.constant('uiDateConfig', {})
.constant('uiDateFormatConfig', '')
.factory('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig) {




return {
stringToDate: stringToDate,
dateToString: dateToString,
};

//https://github.com/angular/angular.js/blob/622c42169699ec07fc6daaa19fe6d224e5d2f70e/src/Angular.js#L1207
function timezoneToOffset(timezone, fallback) {
timezone = timezone.replace(/:/g, '');
var requestedTimezoneOffset = Date.parse('Jan 01, 1970 00:00:00 ' + timezone) / 60000;
return isNaN(requestedTimezoneOffset) ? fallback : requestedTimezoneOffset;
}

function addDateMinutes(date, minutes) {
date = new Date(date.getTime());
date.setMinutes(date.getMinutes() + minutes);
return date;
}

function convertTimezoneToLocal(date, timezone, reverse) {
reverse = reverse ? -1 : 1;
var dateTimezoneOffset = date.getTimezoneOffset();
var timezoneOffset = timezoneToOffset(timezone, dateTimezoneOffset);
return addDateMinutes(date, reverse * (timezoneOffset - dateTimezoneOffset));
}


function doTZ(date,timezone,reverse) {
return timezone ? convertTimezoneToLocal(date, timezone, reverse) : date;
Expand Down Expand Up @@ -105,15 +115,15 @@ export default angular.module('ui.date', [])
return;
}

if (isDate) {
if (isDate && !timezone) {
angular.forEach(keys, function(key) {
preserve[key] = controller.$modelValue['get' + key]();
});
}

var newViewValue = $element.datepicker('getDate');

if (isDate) {
if (isDate && !timezone) {
angular.forEach(keys, (key) => {
newViewValue['set' + key](preserve[key]);
});
Expand Down Expand Up @@ -179,6 +189,7 @@ export default angular.module('ui.date', [])
if (angular.isDate(controller.$modelValue) === false && angular.isString(controller.$modelValue)) {
controller.$modelValue = uiDateConverter.stringToDate(attrs.uiDateFormat, controller.$modelValue, timezone);
}
controller.$modelValue = convertTimezoneToLocal(controller.$modelValue, timezone, true);
$element.datepicker('setDate', controller.$modelValue);
};
}
Expand Down