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

114
Views
Check if an array contains Date, that is between Two Dates in the second array

I have two arrays.

let dateArray = ['2018-05-04T00:00:00+01:00', '2019-04-20T00:00:00+01:00', '2020-05-29T00:00:00+01:00'];

let rangesArray = [['2021-09-01','2022-09-01'],['2019-09-01','2020-09-01']];

How to check if the dates from dateArray are between the dates in rangesArray.

rangesArray[0] is first range - Im interested in dates between 2021-09-01 and 2022-09-01.

rangesArray[1] is second range - Im interested in dates between 2019-09-01 and 2020-09-01.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

use Date.parse() to convert string to date for comparison.

working example:

function dateCheck(from, to, check) {
  var fDate, lDate, cDate;
  fDate = Date.parse(from);
  lDate = Date.parse(to);
  cDate = Date.parse(check);

  if (cDate <= lDate && cDate >= fDate) {
    return true;
  }
  return false;
}

let dateArray = [
  "2018-05-04T00:00:00+01:00",
  "2019-04-20T00:00:00+01:00",
  "2020-05-29T00:00:00+01:00",
];

let rangesArray = [
  ["2021-09-01", "2022-09-01"],
  ["2019-09-01", "2020-09-01"],
];

for (let i = 0; i < dateArray.length; i++) {
  for (let j = 0; j < rangesArray.length; j++) {
    console.log(dateCheck(rangesArray[j][0], rangesArray[j][1], dateArray[i]));
  }
}

explanation: dateCheck function checks if the date is between from date and to date. with two loops you can get the result that you want.

about 4 years ago · Juan Pablo Isaza Report

0

One way :

let dateArray = ['2018-05-04T00:00:00+01:00', '2019-04-20T00:00:00+01:00', '2020-05-29T00:00:00+01:00'];

let rangesArray = [['2021-09-01','2022-09-01'],['2019-09-01','2020-09-01']];

const res = []
dateArray.forEach(d => {
  rangesArray.forEach(a => {
    if ((new Date(d) >= new Date(a[0])) && (new Date(d) <= new Date(a[1]))){
      res.push({date: d, range: a[0] + ' ' + a[1]})
    }
  })
})

console.log(res)

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!