I'm using moment.js to display time on my webpage. I have the html div:
<div id="time"></div>
and the following javascript:
<script>
moment.tz.add('America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0');
var newYork = moment.tz("America/New_York").format('HH:mm a');
$('#time').append( "Current time in New York: "+newYork );
</script>
When I run the page, it gives me the correct time, but it doesn't change with every minute, so even after 10 minutes or so I get the time that was visible when I loaded the page. Is there any way to keep the time updated? Here's my fiddle so far: http://jsfiddle.net/93pEd/132/
Use setInterval()
to run the code every 60 seconds.
Use html()
instead of append()
so that the previous time is overridden.
function updateTime(){
var newYork = moment.tz("America/New_York").format('HH:mm a');
$('#time').html( "Current time in New York: "+newYork );
};
moment.tz.add('America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0');
updateTime();
setInterval(function(){
updateTime();
},60000);
http://jsfiddle.net/93pEd/135/
Heres an example using seconds also:
http://jsfiddle.net/93pEd/136/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With