Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Created October 23, 2017 19:54
Show Gist options
  • Save burdiuz/9d03d44c331f755d06e98a087282cd44 to your computer and use it in GitHub Desktop.
Save burdiuz/9d03d44c331f755d06e98a087282cd44 to your computer and use it in GitHub Desktop.
Simple factory provider for angular >1.4 that returns GMT string for local timezone.
/**
* Created by Oleg Galaburda on 26.09.15.
*/
(function(){
angular.module('timezone.gmt', []).factory('gmtFactory', function() {
function getTimezoneOffset(date){
date = date || new Date();
return date.getTimezoneOffset();
}
function intToString(num, length) {
length = length || 2;
var value = String(num);
while(value.length<length) {
value = '0' + value;
}
return value;
}
function minutesToGMT(num) {
var hours = (num / 60) >> 0;
var minutes = num % 60;
return intToString(hours) + intToString(minutes);
}
function convertOffsetToGMT(offset) {
var value = 'GMT';
if(offset < 0) {
value += '+';
offset = -offset;
}else value += '-';
return value + minutesToGMT(offset);
}
function getGMTString(date) {
return convertOffsetToGMT(getTimezoneOffset(date));
}
getGMTString.toString = getGMTString;
getGMTString.byOffset = convertOffsetToGMT;
return getGMTString;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment