Skip to content

Instantly share code, notes, and snippets.

@singhmohancs
Last active September 28, 2017 09:16
Show Gist options
  • Save singhmohancs/edebf49c2aa0c59f5186a6940663208a to your computer and use it in GitHub Desktop.
Save singhmohancs/edebf49c2aa0c59f5186a6940663208a to your computer and use it in GitHub Desktop.
AngularJS - UTC to Local + Filter
<span>Local date & time converted from UTC: {{vm.myUtcDate | utcToLocal:'dd.MM.yy - hh.mm a'}}</span>
<!-- Where vm.myUtcLocal = '2016-03-31T00:11:31' (utc) -->
(function () {
'use strict';
angular
.module('app')
.filter('utcToLocal', Filter);
function Filter($filter) {
return function (utcDateString, format) {
// return if input date is null or undefined
if (!utcDateString) {
return;
}
// append 'Z' to the date string to indicate UTC time if the timezone isn't already specified
if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) {
utcDateString += 'Z';
}
// convert and format date using the built in angularjs date filter
return $filter('date')(utcDateString, format);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment