Dynamic date and time with moment.js and setInterval

后端 未结 4 1473
忘了有多久
忘了有多久 2021-02-07 12:22

I\'m trying to find out how I can display dynamic date and time using moment.js.

Apparently I can\'t figure out to use setInterval properly.

If possible I\'d pre

4条回答
  •  生来不讨喜
    2021-02-07 12:31

    I've made a few modifications in your code:

    Note that the method update is now outside the ready event handler

    code:

    var datetime = null,
            date = null;
    
    var update = function () {
        date = moment(new Date())
        datetime.html(date.format('ffffdd, MMMM Do YYYY, h:mm:ss a'));
    };
    
    $(document).ready(function(){
        datetime = $('#datetime')
        update();
        setInterval(update, 1000);
    });
    

    working demo: jsfiddle

提交回复
热议问题