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

164
Visualizações
moment.isBetween() is not working as expected

I am using moment (version "^2.25.3") library to check if the date is in between two dates.

I have to check, if araDate is in between two dates(startdate and endDate) or araDateMinusOne is in between the dates(startDate and endDate), but the condition here is startdate should be equal to araDateMinusOne(ie if a startdate is sep/03 and enddate is sep/04, it should not match).

Can somebody tell me what I am doing wrong or why exactly is this happening?

const obj = {
  _id: '614c2b941e06b7003024e1df',
  promotedtitles: [{ title: '1' }, { title: '2' }],
  startdate: '2021-09-04T00:00:00.000Z',
  enddate: '2021-09-04T00:00:00.000Z'
};

const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04

console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[)')); // this is consoling false, which is incorrect

//I also have tried this as well and all the variations.

console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.js" ></script>

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

0

The reason why it is returning false for checking the equal range and giving '[)', as a parameter is the enddate is taking priority here. And when it is checking endDate we are giving exclusion operator for that.

moment('2021-09-04').isBetween('2021-09-04', '2021-09-04', 'day', '[)'

The solution to this problem is to check the date is between two dates and if the startdate is equal to that araMinusOne date.

moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[]') && moment(araDateMinusOne).isSame(camStartDate)
about 4 years ago · Juan Pablo Isaza Relatório

0

the inclusion parameters should be []

const obj = {
  _id: '614c2b941e06b7003024e1df',
  promotedtitles: [{ title: '1' }, { title: '2' }],
  startdate: '2021-09-04T00:00:00.000Z',
  enddate: '2021-09-04T00:00:00.000Z'
};

const camStartDate = moment(obj.startdate).format('YYYY-MM-DD'); // 2021-09-04
const camEndDate = moment(obj.enddate).format('YYYY-MM-DD'); // 2021-09-04
const araDate = moment('2021-09-05T00:00:00.000Z').format('YYYY-MM-DD'); // 2021-09-05
const araDateMinusOne = moment('2021-09-05T00:00:00.000Z').subtract(1, 'day').format('YYYY-MM-DD'); // 2021-09-04

console.log('check', moment(araDate).isBetween(camStartDate, camEndDate, undefined, '[]')); // false which is fine.
console.log({araDateMinusOne,camStartDate, camEndDate})
console.log('another check', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, undefined, '[]')); // this is consoling false, which is incorrect
//I also have tried this as well and all the variations.

console.log('another check 1', moment(araDateMinusOne).isBetween(camStartDate, camEndDate, 'day', '[)'));


console.log('default check ', moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', undefined, '[]'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment.min.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