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

89
Visualizações
Moment diff in months excluding days

I am trying to work out a difference in months between two dates without taking days into consideration.

I was trying to use Math.ceil but if the day in 2022 was ahead of the one in 2021 then I got 2 months instead of 1 month difference.

const diff = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months', true);
console.log(diff); // 0.935483870967742, expected 1

const diffCeil = Math.ceil(
  moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months', true)
);
console.log('diffCeil', diffCeil); // 2, expected 1

// Inaccurate diff doesn't work when 2021 day is bigger than in 2022
const inacurrateDiff = moment([2022, 0, 3]).diff(moment([2021, 11, 1]), 'months');
console.log('inacurrateDiff', inacurrateDiff); // 1, as expected
const inacurrateDiff2 = moment([2022, 0, 1]).diff(moment([2021, 11, 3]), 'months');
console.log('inacurrateDiff2', inacurrateDiff2); // 0, expected 1
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

I did try it with and without diff's third precise parameter.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could use startOf(d, 'month') to compare start of months:

a.startOf('month').diff(b.startOf('month'), 'month');
about 4 years ago · Juan Pablo Isaza Relatório

0

You get 2 because you're using Math.ceil, which always raises the fractional value to the next whole number.

There's no one right answer here, you'll have to decide how much of a fractional month you want to count.

For instance, if you want only whole months, use Math.floor rather than Math.ceil. If you want to count anything up to 1.5 months as one but 1.5 months and above as 2, use Math.round. Or you could set your own threshold, like 1.2 (it's a bit more work, but not that much).

Examples:

function example(date1, date2) {
    const rawDiff = date1.diff(date2, "months", true);
    const diffFloored = Math.floor(rawDiff);
    const diffRounded = Math.round(rawDiff);
    const diffCeiled = Math.ceil(rawDiff);
    const neg = rawDiff < 0;
    const fractional = neg
        ? rawDiff + Math.trunc(rawDiff)
        : rawDiff - Math.trunc(rawDiff);
    const diffOnePointTwo = Math.abs(fractional) > 0.2
        ? Math.ceil(rawDiff)
        : Math.floor(rawDiff);

    console.log({
        date1: date1.toString(),
        date2: date2.toString(),
        rawDiff,
        diffFloored,
        diffRounded,
        diffCeiled,
        diffOnePointTwo,
    });
}

example(
    moment([2022, 0, 1]),
    moment([2021, 11, 3])
);
example(
    moment([2022, 0, 3]),
    moment([2021, 11, 1])
);
example(
    moment([2022, 0, 10]),
    moment([2021, 11, 1])
);
.as-console-wrapper {
    max-height: 100% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

Only you and your project can say what definition you want to use.

about 4 years ago · Juan Pablo Isaza 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