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

93
Views
Checking for date isWeekend for array of dates - Javascript

I would like to check which days are weekends in my array. For now I only managed to check on single dates, and not the whole array at once.

If it is a weekend I would like to change the color on my barchart. Any help would be much appreciated.

const dates = ['2022-07-15', '2022-07-16', '2022-07-17', '2022-07-18', '2022-07-19', '2022-07-20']

function isWeekend(date = new Date()) {
    return date.getDay() === 6 || date.getDay() === 0;
  }

  const d1 = new Date(dates);

  console.log(d1.getDay()); 

  console.log(d1.isWeekend()); 

  const data = {
    labels: dates,
    datasets: [
      {
        label: "Amount of Visitors",
        data: [1, 4, 3, 7, 5, 2],
        backgroundColor: "rgba(255, 99, 132, 0.5)",
      },
    ],
  };
almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your isWeekend function looks good (ignoring time zone support), you likely just need to set the background colors in your bar chart:

const data = {
  labels: dates,
  datasets: [
    {
      label: 'Amount of Visitors',
      data: [1, 4, 3, 7, 5, 2],
      backgroundColor({ dataIndex }) {
        return isWeekend(new Date(dates[dataIndex])) ? 'green' : 'red';
      },
    },
  ],
};

https://codesandbox.io/s/check-for-dates-on-weekends-dv07np?file=/src/Visitors.js

almost 4 years ago · Santiago Trujillo Report

0

const dates = ['2022-07-15', '2022-07-16', '2022-07-17', '2022-07-18', '2022-07-19', '2022-07-20'];
function isWeekEnd(date){
  var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  var day = new Date(date);
  var dayName = days[day.getDay()];
  console.log(dayName);
  switch (dayName) {
    case 'Friday':
      return true
      break;
    case "Saturday":
      return true
      break;
    default:
      return false
  }
}
for(var i=0;i<dates.length;i++){
  console.log(isWeekEnd(dates[i]));
}

almost 4 years ago · Santiago Trujillo 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!