Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a date is a Saturday or a Sunday using JavaScript

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

Do you have the code for this?

like image 584
Malcolm Avatar asked Jul 25 '09 04:07

Malcolm


People also ask

How do I get this week in JavaScript?

var today = new Date(); var startDay = 0; var weekStart = new Date(today. getDate() - (7 + today. getDay() - startDay) % 7); var weekEnd = new Date(today. getDate() + (7 - today.

How do you code a date in JavaScript?

We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax. Syntax: new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hours, minutes, seconds, milliseconds);

What is JavaScript date now ()?

now() The static Date. now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.


1 Answers

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!'); 
like image 164
Andrew Moore Avatar answered Sep 19 '22 02:09

Andrew Moore