Last active
September 28, 2017 09:16
-
-
Save singhmohancs/edebf49c2aa0c59f5186a6940663208a to your computer and use it in GitHub Desktop.
AngularJS - UTC to Local + Filter
This file contains 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
<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) --> |
This file contains 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
(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