Created
October 23, 2017 19:54
-
-
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.
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
/** | |
* 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