Quiero obtener la cuenta regresiva completa restante de la próxima instancia del día de la semana (por ejemplo, el próximo jueves), tengo dos funciones:
PRIMERO: obtiene instancia del próximo jueves
SEGUNDO: calcula los días, horas, minutos y segundos restantes
Primero probé con moment.js pero los minutos y los segundos siempre devuelven 59 sin importar qué, los días y las horas parecen funcionar.
Entonces, dado que el momento está en desuso, necesito hacer esto con date-fns, del cual no sé mucho.
let nextThursday = getNextThursday(); console.log('nextThursday',nextThursday); LOG nextThursday "2022-03-22T14:30:31.514Z getCountdown(nextThursday); LOG Days: 2 LOG Hours: 23 LOG Minutes: 59 LOG Seconds: 59 const getCountdown = (ending) => { var now = moment(); var end = moment(ending); // another date var duration = moment.duration(end.diff(now)); console.log(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); }; LOG Minutes: 59 LOG Seconds: 59 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).utcOffset(0).set({hour:0,minute:0,second:0,millisecond:0}); //return moment().add(1, 'weeks').add(22, 'minutes').add(43, 'seconds').isoWeekday(dayINeed); } };