Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!