Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
How do I get the days between the today's day and the last day of the month using Moment.js?

Here's the code that I have right now:

  const moment = require('moment')
    const m = moment


    const currDay = m().format('D')
    const dayOfWeek = m().format('dddd')
    const daysInMonth = m().daysInMonth()

    const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
    const endOfMonth   = moment().endOf('month').format('YYYY-MM-DD hh:mm');

I need to create a calendar row where the first item would be the todays date, and the rest of the calendar items would be the whatever amount of days are left depending on the current month so I could render each day in between in my HTML with Vue.

Example: Wed 8, Thu 9, Fri 10 ... Fri 31.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I think the OP is tripped up on the common mistake of formatting prematurely. format is good to see an intermediate result, but doing so produces a string that's no good for additional calculation.

Try to handle date objects only. Convert to strings only when you must: (a) presenting to a human reader, or (b) serializing for storage or transmission.

Working without formatting...

const daysRemainingThisMonth = moment().endOf('month').diff(moment(), 'days');
console.log(`There are ${daysRemainingThisMonth} days remaining this month`)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

over 4 years ago · Santiago Trujillo Relatório

0

Just as a POJS equivalent, if you have a function to return the last day of the month, you can use that and just get the difference between the two dates, e.g.

function getMonthEnd(date = new Date()) {
  return new Date(date.getFullYear(), date.getMonth() + 1, 0);
}

function getMonthDaysLeft(date = new Date()) {
  return getMonthEnd(date).getDate() - date.getDate();
}

let d = new Date();
console.log(`There are ${getMonthDaysLeft(d)} days left in ${d.toLocaleString('en',{month:'long'})}.`);

To get a list/array of the days remaining, just loop over a date, adding 1 day at a time, and write the dates in the required format into the list:

function getMonthDaysLeftAsList(date = new Date()) {
  let d = new Date(+date);
  // Formatter
  let f = new Intl.DateTimeFormat('en',{
    day: 'numeric',
    month: 'short'
  });
  let m = d.getMonth();
  let dayList = [];
  while (d.getMonth() == m) {
    dayList.push(f.format(d));
    d.setDate(d.getDate() + 1);
  }
  return dayList;
}

console.log(getMonthDaysLeftAsList());

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda