Skip to content

Instantly share code, notes, and snippets.

@ckenny
Created September 18, 2013 08:42
Show Gist options
  • Save ckenny/6606365 to your computer and use it in GitHub Desktop.
Save ckenny/6606365 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>TimeZone wise Time</title>
<script type="text/javascript">
// function to calculate local time in a different city
// given the city's UTC offset
function calcTime(city, offset) {
// create Date object for current location
d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return city + " : " + nd.toLocaleString();
}
function showTimes() {
// get Bombay time
document.getElementById("div1").innerHTML = calcTime('Bombay', '5.5');
// get Singapore time
document.getElementById("div2").innerHTML = calcTime('Singapore', '8');
// get London time
document.getElementById("div3").innerHTML = calcTime('Hawaii', '-10');
}
</script>
</head>
<body onload="showTimes()">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment