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

128
Views
JavaScript detect if giving region and date is a holiday
const regions = [
  {
    id: 1,
    name: "Baden-Württemberg",
    holidays: HOLIDAYS_BW,
  },
  {
    id: 2,
    name: "Bayern",
    holidays: HOLIDAYS_BY,
  },
]

const HOLIDAYS_BW = [
  { date: "01.01.2022", day: "Samstag" },
  { date: "06.01.2022", day: "Donnerstag" },
];

const HOLIDAYS_BY = [
  { date: "01.01.2022", day: "Samstag" },
  { date: "06.01.2022", day: "Donnerstag" },
];

How can I check wether the region and date the user picks is a holiday.

For instance I would like to check wether the region Bayern and the date 05.05.2022 is a holiday, the array shows all holidays

I tried this so far, but it does not seems to work

regions.find((region) => region.holidays.includes(date));

The date which I gave to the "query" is in this format

 Tue Mar 22 2022 01:00:00 GMT+0100

That is an issue I think

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

0

You can try like so, finding if the region exists first (returns undefined if not), and then if the date is in the list.

return regions.find(r => r.name == region)?.holidays.some(h => h.date == date)

If you don't like the undefined and always want false, just add ?? false at the end.

return regions.find(r => r.name == region)?.holidays.some(h => h.date == date) ?? false

const HOLIDAYS_BW = [{
    date: "01.01.2022",
    day: "Samstag"
  },
  {
    date: "06.01.2022",
    day: "Donnerstag"
  },
];

const HOLIDAYS_BY = [{
    date: "01.01.2022",
    day: "Samstag"
  },
  {
    date: "06.01.2022",
    day: "Donnerstag"
  },
];

const regions = [{
    id: 1,
    name: "Baden-Württemberg",
    holidays: HOLIDAYS_BW,
  },
  {
    id: 2,
    name: "Bayern",
    holidays: HOLIDAYS_BY,
  },
]

function formatDate(date) {
  return `${('0' + (date.getDate())).slice(-2)}.${('0' + (date.getMonth())).slice(-2)}.${date.getFullYear()}`
}

function isHoliday(region, date) {
  var formattedDate = formatDate(date);
  return regions.find(r => r.name == region)?.holidays.some(h => h.date == formattedDate);
}

console.log(isHoliday('Bayern', new Date(2022, 01, 06))); // true
console.log(isHoliday('Baden-Württemberg', new Date(2022, 01, 01))); // true
console.log(isHoliday('Bayern', new Date())); // false
console.log(isHoliday('Hamburg', new Date(2022, 01, 06))); // undefined as Hamburg is not on the list

UPDATE

Turns out the input is a date, you can format it first with something like:

var formattedDate = `${('0' + (date.getDate())).slice(-2)}.${('0' + (date.getMonth())).slice(-2)}.${date.getFullYear()}`

and then use the string for the comparison.

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!