Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda