how to determine if date is weekend in javascript

JavascriptDate

Javascript Problem Overview


if i have a date coming into a function, how can i tell if its a weekend day?

Javascript Solutions


Solution 1 - Javascript

var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek  === 0); // 6 = Saturday, 0 = Sunday

Solution 2 - Javascript

var isWeekend = yourDateObject.getDay()%6==0;

Solution 3 - Javascript

Short and sweet.

var isWeekend = ([0,6].indexOf(new Date().getDay()) != -1);

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Solution 6 - Javascript

var d = new Date();
var n = d.getDay();
 if( n == 6 )
console.log("Its weekend!!");
else
console.log("Its not weekend");

Solution 7 - Javascript

I've tested most of the answers here and there's always some issue with the Timezone, Locale, or when start of the week is either Sunday or Monday.

Below is one which I find is more secure, since it relies on the name of the weekday and on the en locale.

let startDate = start.clone(),
    endDate = end.clone();

let days = 0;
do {
    const weekday = startDate.locale('en').format('dddd'); // Monday ... Sunday
    if (weekday !== 'Sunday' && weekday !== 'Saturday') days++;
} while (startDate.add(1, 'days').diff(endDate) <= 0);

return days;

Solution 8 - Javascript

In the current version, you should use

    var day = yourDateObject.day();
    var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday

Solution 9 - Javascript

Use .getDay() method on the Date object to get the day.

Check if it is 6 (Saturday) or 0 (Sunday)

var givenDate = new Date('2020-07-11');
var day = givenDate.getDay();
var isWeekend = (day === 6) || (day === 0) ? 'It's weekend': 'It's working day';
    
console.log(isWeekend);

Solution 10 - Javascript

The following outputs a boolean whether a date object is during «opening» hours, excluding weekend days, and excluding nightly hours between 23H00 and 9H00, while taking into account the client time zone offset.

Of course this does not handle special cases like holidays, but not far to ;)

let t = new Date(Date.now()) // Example Date object
let zoneshift = t.getTimezoneOffset() / 60
let isopen = ([0,6].indexOf(t.getUTCDay()) === -1) && (23 + zoneshift  < t.getUTCHours() === t.getUTCHours() < 9 + zoneshift)

// Are we open?
console.log(isopen)

<b>We are open all days between 9am and 11pm.<br>
Closing the weekend.</b><br><hr>

Are we open now?

Alternatively, to get the day of the week as a locale Human string, we can use:

let t = new Date(Date.now()) // Example Date object

console.log(
  new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(t) ,
  new Intl.DateTimeFormat('fr-FR', { weekday: 'long'}).format(t) ,
  new Intl.DateTimeFormat('ru-RU', { weekday: 'long'}).format(t)
)

Beware new Intl.DateTimeFormat is slow inside loops, a simple associative array runs way faster:

console.log(
  ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date(Date.now()).getDay()]
)

Solution 11 - Javascript

Simply add 1 before modulo

var isWeekend = (yourDateObject.getDay() + 1) % 7 == 0;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleoraView Question on Stackoverflow
Solution 1 - JavascriptLukeHView Answer on Stackoverflow
Solution 2 - JavascriptkennebecView Answer on Stackoverflow
Solution 3 - Javascriptuser1949536View Answer on Stackoverflow
Solution 4 - JavascriptT04435View Answer on Stackoverflow
Solution 5 - JavascriptOrlandsterView Answer on Stackoverflow
Solution 6 - JavascriptPraveenView Answer on Stackoverflow
Solution 7 - JavascriptfelippeView Answer on Stackoverflow
Solution 8 - JavascriptAaron NusselbaumView Answer on Stackoverflow
Solution 9 - JavascriptTanzeem BhattiView Answer on Stackoverflow
Solution 10 - JavascriptNVRMView Answer on Stackoverflow
Solution 11 - JavascriptMuhammad UsamaView Answer on Stackoverflow