How to determine if date is weekend in JavaScript
Asked Answered
S

12

106

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

Sniperscope answered 23/8, 2010 at 21:20 Comment(4)
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 en.wikipedia.org/wiki/Workweek_and_weekendYuri
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 languageCeroplastics
@Yuri True see latest simple answer taking care of other countries.Wife
@Ceroplastics please see my latest answer to fix iyWife
H
215
var dayOfWeek = yourDateObject.getDay();
var isWeekend = (dayOfWeek === 6) || (dayOfWeek  === 0); // 6 = Saturday, 0 = Sunday
Hogle answered 23/8, 2010 at 21:25 Comment(7)
d != day :) I would rather call it dayOfWeek, it would make more sense to OP.Sachi
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.Gamali
@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.)Hogle
For js, it's probably better if you do === instead of == when comparing absolute values. Not crucial, but just best practice.Knossos
See ecma-international.org/ecma-262/6.0/#sec-week-day. 0 always equals SundayRadford
Some countries have Friday and Saturday as weekend so this answer is suitable for the Christian Stackoverflow :-pYuri
@Hogle a simple modern solution that can handle all Locales is posted. Hope this works for youWife
E
63
var isWeekend = yourDateObject.getDay()%6==0;
Eavesdrop answered 23/8, 2010 at 21:31 Comment(6)
This says returns true if it's SaturdayVillanueva
0%6(Sunday) and 6%6 (Saturday) both have a 0 modulusEavesdrop
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.Siltstone
Some countries have Friday and Saturday as weekendYuri
@Yuri 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.Lorrinelorry
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.Yuri
P
13

Short and sweet.

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

Phenomenalism answered 11/3, 2015 at 18:20 Comment(1)
or [0, 6].includes(new Date().getDay())Gamelan
N
10

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.

Neel answered 7/11, 2018 at 3:56 Comment(3)
Some countries have Friday and Saturday as weekendYuri
@Yuri 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). Neel
You do not need to use libraries. Simple JS one line code will work for all weekends in any country. Please see latest post.Wife
W
3

Update Nov 2023

Since mid-2022, the days of the weekend can be obtained using the weekInfo method of Intl.Locale instances.

The method is supported in all modern browsers (under Windows, iOS, and Android including Node.js and Deno, with the exception of Firefox as of Nov 2023).

new Intl.Locale(locale).weekInfo.weekend will return an array of integers indicating the days of the weekend for the particular locale, where 1 is Monday and 7 is Sunday.

Further information can be found on MDN Intl.Locale.prototype.getWeekInfo().

Examples:

console.log(new Intl.Locale("en-US").weekInfo.weekend);
//[6,7]  // weekend is days 6 and 7 (Sat and Sun) in USA

console.log(new Intl.Locale("ar-EG").weekInfo.weekend);
// [5,6]  // weekend is days 5 and 6 (Fri and Sat) in Egypt

console.log(new Intl.Locale("en-IN").weekInfo.weekend);
// [7]  // weekend is day 7 (Sunday) in India

Unlike the Date.getDay() method which returns the weekday number with 0 meaning Sunday, the weekInfo.weekend returns Sunday as day number 7. Monday is day 1.

The function below tests, if the weekday number returned from Date.getDay() (after correction for Sunday) is included in the array of integers returned from the weekInfo.weekend method.

function isWeekend(date , locale="en-US") {
return new Intl.Locale(locale).weekInfo.weekend.includes(date.getUTCDay()||7);
}

//==== test for 2023-11-24 (Friday) =======
let myDate = new Date("2023-11-24");
console.log(isWeekend(myDate));         // false in USA
console.log(isWeekend(myDate,"ar-EG")); // true  in Egypt
console.log(isWeekend(myDate,"en-IN")); // false in India
console.log(isWeekend(myDate,"fa-IR")); // true  in Iran

The following table summarises the days of the workweek and weekends as detailed on table of Wikipedia page.

Total Countries Days of Weekend Total Working Days
95 Saturday and Sunday 5
18 Friday and Saturday 5
1 Friday and Sunday 5
10 Sunday 6
4 Friday 6
1 Saturday 6
Source: Wikipedia (Nov 2023)
Wife answered 26/11, 2023 at 8:37 Comment(2)
ATM this is unfortunately NOT supported by Firefox.Moores
True. I have already stated this in my answer above. Thanks.Wife
R
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
Ret answered 26/3, 2020 at 18:38 Comment(1)
.isoWeekday() is a moment.js method, but not specified.Meraz
A
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;
Atabrine answered 8/7, 2019 at 16:39 Comment(0)
I
0

In the current version, you should use

    var day = yourDateObject.day();
    var isWeekend = (day === 6) || (day === 0);    // 6 = Saturday, 0 = Sunday
Interlunar answered 23/4, 2020 at 16:21 Comment(0)
D
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);
Dourine answered 12/7, 2020 at 4:18 Comment(0)
N
-1
var d = new Date();
var n = d.getDay();
 if( n == 6 )
console.log("Its weekend!!");
else
console.log("Its not weekend");
Nahshunn answered 24/2, 2016 at 6:4 Comment(0)
M
-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()]
)
Meraz answered 23/10, 2020 at 21:31 Comment(0)
R
-3

Simply add 1 before modulo

var isWeekend = (yourDateObject.getDay() + 1) % 7 == 0;
Renaerenaissance answered 24/10, 2014 at 0:47 Comment(3)
This works only if you consider Sunday to be the whole weekend.Geodesic
@cpburnz most of the countries do. only a few start the week with sunday.Important
@Important Then that's a vital piece of information that should be added to your answer.Geodesic

© 2022 - 2024 — McMap. All rights reserved.