Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
Issue getting remaining days,hours/minutes.. to date with moment

I have two functions in my app, one where I get the instance of the next weekday I choose (get me next thursday for example), then I have a function which calculates remianing days,hours,minutes and seconds.

So far I can get remaining days, but hours, minutes and seconds show as 0. This is because when the date I get for next thursday doesnt have the required timestamp format:

nextThursday "2022-03-24T15:11:48.580Z"

 LOG  Days:  5.999999745370371
 LOG  Hours:  0
 LOG  Minutes:  0
 LOG  Seconds:  0

HOW CAN I ADD HOURS AND MINUTES (00:00) to the moment timestamp I get, I think that would be the problem!

let nextThursday = getNextThursday();
getCountdown();

const getNextThursday = () =>
    {
        const dayINeed = 4; // for Thursday
        const today = moment().isoWeekday();

        // if we haven't yet passed the day of the week that I need:
        if (today <= dayINeed) { 
        // then just give me this week's instance of that day
        return moment().isoWeekday(dayINeed);
        } else {
         // otherwise, give me *next week's* instance of that same day
        return moment().add(1, 'weeks').isoWeekday(dayINeed);
        }
    };

    const getCountdown = (ending) =>
    {
        var now = moment();
var end = moment(ending); // another date
var duration = moment.duration(end.diff(now));

//Get Days and subtract from duration
var days = duration.asDays();
duration.subtract(moment.duration(days,'days'));

//Get hours and subtract from duration
var hours = duration.hours();
duration.subtract(moment.duration(hours,'hours'));

//Get Minutes and subtract from duration
var minutes = duration.minutes();
duration.subtract(moment.duration(minutes,'minutes'));

//Get seconds
var seconds = duration.seconds();
console.log("Days: ",days);
console.log("Hours: ",hours);
console.log("Minutes: ",minutes);
console.log("Seconds: ",seconds);
    };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your problem is you're using asDays() which converts your duration to a decimal number (including hours, minutes, and seconds in decimal part)

And you don't need to call moment.duration(x,'x') in every subtraction

The fix should be

const getCountdown = (ending) => {
  var now = moment();
  var end = moment(ending); // another date
  var duration = moment.duration(end.diff(now));

  //Get Days and subtract from duration
  var days = duration.days();
  duration.subtract(days, 'days');

  //Get hours and subtract from duration
  var hours = duration.hours();
  duration.subtract(hours, 'hours');

  //Get Minutes and subtract from duration
  var minutes = duration.minutes();
  duration.subtract(minutes, 'minutes');

  //Get seconds
  var seconds = duration.seconds();
  console.log("Days: ", days);
  console.log("Hours: ", hours);
  console.log("Minutes: ", minutes);
  console.log("Seconds: ", seconds);
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda