determine whether a date falls on a weekday or weekend in javascript

To determine whether a date falls on a weekday or a weekend in JavaScript, you can use the getDay() method of the Date object. The getDay() method returns a number representing the day of the week, with 0 for Sunday, 1 for Monday, and so on.

Here's an example function that takes a date as input and returns a string indicating whether it falls on a weekday or a weekend:

index.tsx
function getDayOfWeek(date) {
  const dayOfWeek = date.getDay();
  if (dayOfWeek === 0 || dayOfWeek === 6) {
    return 'weekend';
  } else {
    return 'weekday';
  }
}
170 chars
9 lines

You can use this function like this:

index.tsx
const myDate = new Date('2022-01-01');
const dayOfWeek = getDayOfWeek(myDate);
console.log(`The date ${myDate.toDateString()} is on a ${dayOfWeek}.`);
// Output: The date Sat Jan 01 2022 is on a weekend.
204 chars
5 lines

gistlibby LogSnag