简体   繁体   中英

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);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM