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>

2 answers

  • answered 2021-09-23 12:08 Yash Jaiswal

    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)
    

  • answered 2021-09-23 12:09 Harshit Rastogi

    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>

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum