If I have a date coming into a function, how can I tell if it's a weekend day?
-
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 Answers
var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek === 0); // 6 = Saturday, 0 = Sunday
-
10`d` != `day` :) I would rather call it `dayOfWeek`, it would make more sense to OP. – BalusC Aug 23 '10 at 21:26
-
5This 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
-
1For 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
-
1See https://www.ecma-international.org/ecma-262/6.0/#sec-week-day. 0 always equals Sunday – TreeAndLeaf Nov 09 '18 at 05:45
-
6Some countries have Friday and Saturday as weekend so this answer is suitable for the Christian Stackoverflow :-p – Guy Jan 29 '19 at 15:26
var isWeekend = yourDateObject.getDay()%6==0;
- 102,654
- 32
- 106
- 127
-
-
14
-
9except 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
-
-
@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
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.
- 12,507
- 5
- 54
- 54
-
1
-
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
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
- 4,706
- 2
- 30
- 45
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;
- 493
- 6
- 7
In the current version, you should use
var day = yourDateObject.day();
var isWeekend = (day === 6) || (day === 0); // 6 = Saturday, 0 = Sunday
- 529
- 1
- 4
- 11
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);
- 90
- 1
- 8
- 26
- 219
- 2
- 3
var d = new Date();
var n = d.getDay();
if( n == 6 )
console.log("Its weekend!!");
else
console.log("Its not weekend");
- 93
- 1
- 2
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()]
)
- 11,480
- 1
- 88
- 87
Simply add 1 before modulo
var isWeekend = (yourDateObject.getDay() + 1) % 7 == 0;
- 247
- 1
- 2
- 8
-
5This works only if you consider Sunday to be the whole weekend. – Uyghur Lives Matter Oct 24 '14 at 01:32
-
@cpburnz most of the countries do. only a few start the week with sunday. – bokkie Dec 16 '14 at 15:24
-
2@bokkie Then that's a vital piece of information that should be added to your answer. – Uyghur Lives Matter Dec 16 '14 at 16:21