0

I have an object that contains a property of date with a value of 2020-09-02T00:00:00.000Z. I want this value to be compared if it is within the current date or not, meaning if it is within 2020-09-01 00:00:00.00 to 2020-09-01 11:59:59.99. How will I be able to achieve this? This is my initial line of codes. Hope you can help me. Cheers!

 for(let a = 0, c = arr3.length; a < c; a++){
    let olddate = arr3[a].scheduled_time_start.valueOf();
  }

1 Answers1

-1

You can compare native Date objects with comparison (<, <=, >, >=) operators.

const isDateInbetween = (date, startDate, endDate) =>
  date > startDate && date < endDate;

const pastDate   = new Date('2020-09-01T00:00:00.000Z');
const futureDate = new Date('2020-09-01T11:59:59.999Z');
const currDate   = new Date('2020-09-02T00:00:00.000Z');

console.log(isDateInbetween(currDate, pastDate, futureDate)); // false
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • `Date` objects, compared with either "equals" operator, will use reference comparison, not value comparison. Also, please don't answer duplicates. – Heretic Monkey Sep 01 '20 at 16:16
  • @HereticMonkey I did not say to use the equals operator, I was just listing the operators as examples of "comparison operators". Anyways, I removed them to reduce confusion. – Mr. Polywhirl Sep 01 '20 at 16:18