-
-
Save dprothero/224a4752faa6c1797526 to your computer and use it in GitHub Desktop.
DatePicker directive to fix timezone issues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.directive('datepickerLocaldate', [function () { | |
var directive = { | |
require: 'ngModel', | |
link: link | |
}; | |
return directive; | |
function link(scope, element, attr, ngModel) { | |
scope.$watch( | |
function(){ | |
return ngModel.$modelValue; | |
}, | |
function(modelValue){ | |
if (modelValue && !(modelValue instanceof Date)) { | |
var dt = new Date(modelValue); | |
if(dt.getTimezoneOffset() > 0) | |
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset()); | |
ngModel.$modelValue = dt; | |
ngModel.$render(); | |
} | |
}); | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find this works better than just doing the conversion on the first pass. This handles two additional cases that the original gist did not: