Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  175] [ 3]  / answers: 1 / hits: 19318  / 12 Years ago, sat, may 5, 2012, 12:00:00

I found the Momentjs library which is pretty cool, however I don't find the documentation to be very clear on how to achieve some simple tasks.
I'm trying to build a countdown timer and I'm guessing I should use the duration object, but I don't quite understand how (maybe due to the fact that English isn't my first language). Anyways this is what I want:

var time = 7200;
var duration = moment.duration('seconds',time);

//show how many hours, minutes and secods are left
//this doesn't work because there's no method format for the duration object.

So everysecond it should display:







How would I achieve this result with the Momentjs library?

More From » jquery


duration object represents a static period, and it does not increase/decrease with the flow of time. So if you what to decrease it you have to do it yourself, for example creating a kind of a seconds counter or recreating duration object every time. Here is the code for the second option:

var time = 7200;
var duration = moment.duration(time * 1000, 'milliseconds');
var interval = 1000;

duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
//show how many hours, minutes and seconds are left
}, interval);

[#85771] Friday, May 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 415
Total Questions: 117
Total Answers: 94

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago