100

If I have a date coming into a function, how can I tell if it's a weekend day?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • Note that some countries have Friday and Saturday as weekend (as I've mentioned in the answers) so an answer should consider weekend by country https://en.wikipedia.org/wiki/Workweek_and_weekend – Guy Jan 29 '19 at 15:27
  • Is there any way to know if a day is a weekend regardless in which locale we are in, for example using moment.js? Example - Arabic language – Marek Oct 24 '22 at 14:08

11 Answers11

206
var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek  === 0); // 6 = Saturday, 0 = Sunday
Pavel P
  • 15,789
  • 11
  • 79
  • 128
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 10
    `d` != `day` :) I would rather call it `dayOfWeek`, it would make more sense to OP. – BalusC Aug 23 '10 at 21:26
  • 5
    This is *not* true for all timezones. For example, in France, the first day of the week will be Monday, not Sunday. Modern time libraries like Moment compensate for this. – csvan Jun 16 '15 at 06:13
  • 8
    @csvan: `getDay` should always return 0 for sunday and 6 for saturday etc, according to the current timezone settings. (And then it's up to the OP to decide what constitutes a "weekend" according to their requirements.) – LukeH Jun 17 '15 at 11:18
  • 1
    For js, it's probably better if you do `===` instead of `==` when comparing absolute values. Not crucial, but just best practice. – dylanh724 May 10 '18 at 12:21
  • 1
    See https://www.ecma-international.org/ecma-262/6.0/#sec-week-day. 0 always equals Sunday – TreeAndLeaf Nov 09 '18 at 05:45
  • 6
    Some countries have Friday and Saturday as weekend so this answer is suitable for the Christian Stackoverflow :-p – Guy Jan 29 '19 at 15:26
62
var isWeekend = yourDateObject.getDay()%6==0;
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • This says returns true if it's Saturday – Gianfranco P. Nov 03 '13 at 23:14
  • 14
    0%6(Sunday) and 6%6 (Saturday) both have a 0 modulus – kennebec Nov 04 '13 at 01:05
  • 9
    except being voluntary confusing, I don't see any point in this technique. I personally prefer LukeH's answer. It's only by chance that in this case we can use modulo of 6 instead of 7 to solve our problem. – HLP Sep 07 '14 at 01:45
  • Some countries have Friday and Saturday as weekend – Guy Jan 29 '19 at 15:26
  • @Guy then the question is if `.getDay()` will result in another value or if the definition of `isWeekend` would be wrong. If its about the variable, I dont care. I guess a 0 will always be sunday, so its fine for me. – C4d Jul 02 '19 at 14:25
  • Yes, Sunday will always be 0. It is a matter of UX. If you would like to show a different UI for weekends considering the country's settings is important. For instance in Google Calendar you can choose whether to start the week on Sunday or Monday. – Guy Jul 04 '19 at 15:12
13

Short and sweet.

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

user1949536
  • 574
  • 5
  • 6
9

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.

T04435
  • 12,507
  • 5
  • 54
  • 54
  • 1
    Some countries have Friday and Saturday as weekend – Guy Jan 29 '19 at 15:26
  • 1
    @Guy You need to adapt the code to meet country needs. As per the wiki you link above some other countries have a single day weekend. `Some countries have adopted a one-day weekend, i.e. either Sunday only (in seven countries), Friday only (in Djibouti, Iran, Palestine and Somalia), or Saturday only (in Nepal). ` – T04435 Jan 29 '19 at 23:22
2

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
Orlandster
  • 4,706
  • 2
  • 30
  • 45
1

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;
felippe
  • 493
  • 6
  • 7
0

In the current version, you should use

    var day = yourDateObject.day();
    var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday
Damian Tokarczyk
  • 529
  • 1
  • 4
  • 11
0

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);
kibe
  • 90
  • 1
  • 8
  • 26
Tanzeem Bhatti
  • 219
  • 2
  • 3
-1
var d = new Date();
var n = d.getDay();
 if( n == 6 )
console.log("Its weekend!!");
else
console.log("Its not weekend");
Praveen
  • 93
  • 1
  • 2
-1

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()]
)
NVRM
  • 11,480
  • 1
  • 88
  • 87
-3

Simply add 1 before modulo

var isWeekend = (yourDateObject.getDay() + 1) % 7 == 0;
Muhammad Usama
  • 247
  • 1
  • 2
  • 8