Last active
November 29, 2018 16:33
-
-
Save khan-rizwan/de8f84c14ed16f37c6fb994d133062d1 to your computer and use it in GitHub Desktop.
Get difference between two timezones
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
console.log(getTZDiff('UTC+5:30','UTC+8:00')); | |
function getTZDiff(currentTz, otherTz) { | |
var selectedTz = new Date('January 1, 1970 00:00:00 ' + currentTz); | |
var targetTz = new Date('January 1, 1970 00:00:00 ' + otherTz); | |
var difference = selectedTz.getTime() - targetTz.getTime(); | |
var hours = (difference / 3600000); | |
if (hours < 0) return Math.abs(hours) + " hours behind"; | |
return Math.abs(hours) + " hours ahead"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment