how to determine if date is weekend in javascript [closed]
var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek === 0); // 6 = Saturday, 0 = Sunday
var isWeekend = yourDateObject.getDay()%6==0;
Short and sweet.
var isWeekend = ([0,6].indexOf(new Date().getDay()) != -1);
I tried the Correct answer and it worked for certain locales but not for all:
In momentjs Docs: weekday The number returned depends on the locale initialWeekDay, so Monday = 0 | Sunday = 6
So I change the logic to check for the actual DayString('Sunday')
const weekday = momentObject.format('dddd'); // Monday ... Sunday
const isWeekend = weekday === 'Sunday' || weekday === 'Saturday';
This way you are Locale independent.
Update 2020
There are now multiple ways to achieve this.
1) Using the day
method to get the days from 0-6:
const day = yourDateObject.day();
// or const day = yourDateObject.get('day');
const isWeekend = (day === 6 || day === 0); // 6 = Saturday, 0 = Sunday
2) Using the isoWeekday
method to get the days from 1-7:
const day = yourDateObject.isoWeekday();
// or const day = yourDateObject.get('isoWeekday');
const isWeekend = (day === 6 || day === 7); // 6 = Saturday, 7 = Sunday