I am displaying a list of names and their time zone offset numbers in a table. The offset numbers are their time zone's offset from UTC. I need to change all of the offset numbers to show the current time in the person's time zone using JavaScript or jQuery.
What I would like to happen is the offset numbers would be replaced with the current date/time from the calcTime
function... any idea what I'm doing wrong?
Here's my code:
<div class="name">John Doe</div><div class="timezone">-5</div>
<div class="name">Jane Doe</div><div class="timezone">3</div>
<div class="name">Jim Smith</div><div class="timezone">7</div>
<div class="name">Cathy Jones</div><div class="timezone">-3</div>
<script>
$(document).ready(function() {
setTimeout(() => {
function calcTime(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 "The local time is " + nd.toLocaleString();
}
$(".timezone").each(function(){
h = $(this).find(".timezone").text();
element.innerHTML = calcTime(h);
//alert(calcTime('-5'));
});
}, 1000);
});
</script>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…