Skip to content

Instantly share code, notes, and snippets.

@khan-rizwan
Last active November 29, 2018 16:33
Show Gist options
  • Save khan-rizwan/de8f84c14ed16f37c6fb994d133062d1 to your computer and use it in GitHub Desktop.
Save khan-rizwan/de8f84c14ed16f37c6fb994d133062d1 to your computer and use it in GitHub Desktop.
Get difference between two timezones
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