Moment.js dynamically update time in seconds
19,960
Solution 1
it seems to work fine for me. For the same code. And only the moment.min.js pointed to v2.11.0
Have a look at the code(credits: to MilkyWayJoe)-
var datetime = null,
date = null;
var update = function () {
date = moment(new Date())
datetime.html(date.format('dddd, MMMM Do YYYY, h:mm:ss a'));
};
$(document).ready(function(){
datetime = $('#datetime')
update();
setInterval(update, 1000);
});
Working fiddle with v2.11.0 - JSFiddle Demo
Solution 2
A working compact solution.
HTML
<div id="datetime"></div>
JavaScript
var update = function() {
document.getElementById("datetime")
.innerHTML = moment().format('MMMM Do YYYY, h:mm:ss a');
}
setInterval(update, 1000);
Author by
blahblahblah
Updated on June 11, 2022Comments
-
blahblahblah almost 2 years
I'm trying to display a clock that updates every second (e.g. 1/5/2015 12:05:01 then 1/5/2015 12:05:02, etc.) using moment.js.
I found previous solutions to my question (see Dynamic date and time with moment js and setinterval), however it doesn't work with the newest version of moment.js (v2.11.0). The solution provided uses v2.6.
Can someone advise?