Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:11:08+00:00 2026-06-03T06:11:08+00:00

I found the Momentjs library which is pretty cool, however I don’t find the

  • 0

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

setInterval(function(){
  //show how many hours, minutes and secods are left
  $('.countdown').text(duration.format('h:mm:ss')); 
  //this doesn't work because there's no method format for the duration object.
},1000);

So everysecond it should display:

02:00:00

01:59:59

01:59:58

01:59:57

…

00:00:00

How would I achieve this result with the Momentjs library?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:11:09+00:00Added an answer on June 3, 2026 at 6:11 am

    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;
    
    setInterval(function(){
      duration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds');
      //show how many hours, minutes and seconds are left
      $('.countdown').text(moment(duration.asMilliseconds()).format('h:mm:ss'));
    }, interval);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found out about this cool jQuery countdown which is initialized as such: <script type="text/javascript">
Found some nice Open Source code of a snake game, but wanting to make
Found a piece of code today, that I find a little smelly... TMyObject.LoadFromFile(const filename:
Found this very interesting code on total variation filter tvmfilter The additional functions this
Found the function which position the image(s) in the multiscaleimage... but I'm not sure
Found myself trying to find a link to an official definition of this design
Found a question which already explains this - How to sort an array of
Found the documentation here http://www.trirand.com/jqgridwiki/doku.php?id=wiki:toolbar_searching I want JQgrid filterToolbar blank top row in the
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
Found the GUI programming bit difficult to grasp. Looking for some good materials/books/lecture_videos/advice for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.