Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  118] [ 3]  / answers: 1 / hits: 32006  / 12 Years ago, mon, may 14, 2012, 12:00:00

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 prefer not to use jQuery as moment.js dosn't need it.



Here's what I have so far: http://jsfiddle.net/37fLF/2/.



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

More From » jquery

 Answers
24

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('dddd, MMMM Do YYYY, h:mm:ss a'));
};

$(document).ready(function(){
datetime = $('#datetime')
update();
setInterval(update, 1000);
});


working demo: jsfiddle


[#85582] Sunday, May 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;