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

320
Visualizações
JS .moment() - Get next renewal date (monthly & yearly) based on effective and current date

I'm trying to get the next 'renewal date' of a subscription model (month & year). That's my setup:

var getPeriod = 'month';
// var getPeriod = 'year';

var getEffective = '2022-06-02';
var getDate = moment('2022-08-01');

if(getPeriod == 'month'){
    var getDuration = moment.duration(getDate.diff(getEffective)).months();
} else if('year'){
    var getDuration = moment.duration(getDate.diff(getEffective)).years();
}

if(getDuration >= 0){
    var getCounter = getDuration+1;
} else {
    var getCounter = 1;
}

var getRenewalNext = moment(getEffective).add(getCounter, getPeriod+'s').format('YYYY-MM-DD');

console.log('getDuration:');
console.log(getDuration);

console.log('getRenewalNext:');
console.log(getRenewalNext);

Attempt 1: Correct

var getEffective = '2022-06-01';
var getDate = moment('2022-08-01');

"getDuration:"
2
"getRenewalNext:"
"2022-09-01"

Attempt 2: Correct

var getEffective = '2022-06-03';
var getDate = moment('2022-08-01');

"getDuration:"
1
"getRenewalNext:"
"2022-08-03"

Attempt 3: Not Correct

var getEffective = '2022-06-02';
var getDate = moment('2022-08-01');

"getDuration:"
2
"getRenewalNext:"
"2022-09-02"

For the last attempt I'm getting "2022-09-02" as the next renewal data, but I would have expected 1 more day to go and 'Next Renewal Date' to be "2022-08-02". Why so or am I missing something? How can I solve this?

That's my fiddle: https://jsfiddle.net/ja1k6owb/

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

0

Upgrading to a newer version of moment should resolve the issue.

I'm using the latest version below and we're getting the correct answer.

One should be able to find the change made (between moment 2.2.1 and 2.29.4) that fixes the issue.

var getPeriod = 'month';
// var getPeriod = 'year';

var getEffective = '2022-06-02';
var getDate = moment('2022-08-01');

if(getPeriod == 'month'){
    var getDuration = moment.duration(getDate.diff(getEffective)).months();
} else if('year'){
    var getDuration = moment.duration(getDate.diff(getEffective)).years();
}

if(getDuration >= 0){
    var getCounter = getDuration+1;
} else {
    var getCounter = 1;
}

var getRenewalNext = moment(getEffective).add(getCounter, getPeriod+'s').format('YYYY-MM-DD');

console.log('getDuration:');
console.log(getDuration);

console.log('getRenewalNext:');
console.log(getRenewalNext);
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>

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