Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js dynamically update time in seconds

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?

like image 461
blahblahblah Avatar asked Jan 06 '16 04:01

blahblahblah


1 Answers

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

like image 109
bozzmob Avatar answered Sep 24 '22 10:09

bozzmob