Determine if a date is a Saturday or a Sunday using JavaScript
Asked Answered
G

7

84

Is it possible to determine if a date is a Saturday or Sunday using JavaScript?

Do you have the code for this?

Giavani answered 25/7, 2009 at 4:42 Comment(0)
M
187

Sure it is! The Date class has a function called getDay() which returns a integer between 0 and 6 (0 being Sunday, 6 being Saturday). So, in order to see if today is during the weekend:

var today = new Date();
if(today.getDay() == 6 || today.getDay() == 0) alert('Weekend!');

In order to see if an arbitrary date is a weekend day, you can use the following:

var myDate = new Date();
myDate.setFullYear(2009);
myDate.setMonth(7);
myDate.setDate(25);

if(myDate.getDay() == 6 || myDate.getDay() == 0) alert('Weekend!');
Monogamist answered 25/7, 2009 at 4:50 Comment(2)
@Steve: I know, but for examples, it's usually better to be more verbose, in case someone doesn't know the order of the arguments.Monogamist
Great answer @AndrewMoore!! Also worth mentioning that if you set the date manually, you need to subtract the month by 1 because it's a 0 index deal. So 0 = January, 1 = February and so on. :)Vasiliki
X
42

You can simplify @Andrew Moore 's test even further:

if(!(myDate.getDay() % 6)) alert('Weekend!');

(Love that modulo function!)

Xavler answered 20/6, 2013 at 18:52 Comment(2)
Smart one :) I like it.Seafaring
This is a more readable version: const isWeekend = (date) => date.getDay() % 6 === 0Pridemore
A
10

The Date class offers the getDay() Method that retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc)

var date = new Date();
switch(date.getDay()){
    case 0: alert("sunday!"); break;
    case 6: alert("saturday!"); break;
    default: alert("any other week day");
}
Amish answered 25/7, 2009 at 4:49 Comment(0)
O
4

I think this is an elegant way to do this:

function showDay(d) {
    return ["weekday", "weekend"][parseInt(d.getDay() / 6)];
}

console.log(showDay(new Date()));
Odum answered 20/9, 2017 at 11:24 Comment(1)
It doesn't quite work. It works for Saturday but Sunday returns as a weekday, not a weekend.Pridemore
E
3

Yes, it is possible, we can write a JavaScript code for that using JavaScript Date object.

Please use following JavaScript code.

var d = new Date()

document.write(d.getDay())

We can write a function to return the weekend in flag like below, You can more customize the function to pass date. Or different return values for every day.

    isItWeekEnd = function() {
    var d = new Date();
    console.log(d.getDay());
    var dateValue = d.getDay(); 
    // dateValue : 0 = Sunday
    // dateValue : 6 = Saturday
    if(dateValue == 0 || dateValue == 6)
        return true;
    else 
        return false;  
}
Eurasian answered 25/7, 2009 at 4:47 Comment(1)
where is sat & sunday?Lee
D
0

Expanding the modulo answer... this one considers a range and attempt with performance as need to loop:

export function getNumberWeekendsInRange(from: Date, to: Date): number {
  // 86400000 ms is 1 day
  const nFromDays = ~~(Number(from) / 86400000);
  const nToDays = ~~(Number(to) / 86400000);
  let numberOfWeekends = 0;

  for (let epochDay = nFromDays; epochDay < nToDays; epochDay++) {
    // epoch starts on a Thursday
    if (epochDay % 7 === 2 || epochDay % 7 === 3) ++numberOfWeekends;
  }

  return numberOfWeekends;
}
Deaver answered 13/11, 2023 at 21:14 Comment(0)
C
-1

var date = new Date();
var day = date.getDay();
if(day==0){
	return false;
    //alert('sunday'); 
}
Caird answered 26/12, 2014 at 11:15 Comment(1)
How does this add anything to the existing answers? (Let alone that it doesn't answer the full question)Tehuantepec

© 2022 - 2024 — McMap. All rights reserved.