Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Moment.js getting duration from now until future date

I am trying to implement a countdown timer by getting each part of the time until the event (minutes, seconds, etc), with support for timezones. I cannot seem to figure out how to get Moment.js to get the duration between now and a future dated Moment object.

Here is what I have tried:

var eventStartDatetime = moment.parseZone('2022 07 02 13 -05:00', 'YYYY MM DD HH ZZ')
var monthsNum = $("#monthsNum");
var daysNum = $("#daysNum");
var hoursNum = $("#hoursNum");
var minutesNum = $("#minutesNum");
var secondsNum = $("#secondsNum");

console.log(eventStartDatetime.toString())

function updateCountdown(){
    // get difference between user's time (and zone) and the event time
    var difference = moment.duration(eventStartDatetime, 'seconds');
    
    monthsNum.text(difference.months());
    daysNum.text(difference.days());
    hoursNum.text(difference.hours());
    minutesNum.text(difference.minutes());
    secondsNum.text(difference.seconds());
}
updateCountdown();

window.setInterval(function(){
    console.log("Doing repeated update.");
    updateCountdown();
}, 5000);

I have verified that the eventStartDateTime is correct, as the log statement matches what is put in. However, the moment.duration() call seems to give back the incorrect duration, and never has a value for minutes or seconds.

I'm sure I am close, but am missing some detail. I am also having trouble making heads or tails of the Moment documentation.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!