yaser
yaser

Reputation: 199

Update the time provided using moment.js

i have a function where i get the utc time from the database like "2015-10-28T18:02:44" and i want to update the time using set interval in moment.js so that it gives me the output like 8:02:53 am and after a second 8:02:54 am just like a normal clock. so far i have this code which doesn't seems to work

function update() {
    var date = moment(new Date("2015-10-28T18:02:44"));
    console.log(date.format('h:mm:ss a'));
};

setInterval(function(){
    update();
}, 1000);

Upvotes: 0

Views: 1317

Answers (1)

aarjithn
aarjithn

Reputation: 1181

Get the date before the update function, and within it just add a second and then format it.

var date = moment.utc("2015-10-28T18:02:44").local();

function update() {
    console.log(date.add(1, "s").format('h:mm:ss a'));
};

setInterval(function(){
    update();
}, 1000);

JSFiddle

Make sure the setInterval is set to 1000 (1s)

Upvotes: 1

Related Questions