markzzz
markzzz

Reputation: 47945

How can I know if a Date is Saturday?

I have this date as string on client side :

var myDate = "08/08/2012";

how can I know if this date is Saturday or not?

Upvotes: 1

Views: 106

Answers (4)

user1584103
user1584103

Reputation: 193

The Date class has a function called getDay() which returns a integer between 0 and 6 (0 being Sunday, 6 being Saturday).

var today = new Date("08/08/2012");
if(today.getDay() == 6 ) alert('Saturday!');

Upvotes: 0

Jamiec
Jamiec

Reputation: 136114

You need to turn that string into a javascript date and use the getDay() function which will return a number from 0-6 (Sunday being 0, Monday 1 etc).

For the first part of that, you should split up your string and construct a Date object (Im not sure if your date is dd/mm/yyyy or mm/dd/yyyy and the same will happen on client computers so be specific). The easiest way by far is to use a library such as date.js which would allow you to use code such as

var date = Date.parseExact("8/8/2012", "dd/MM/yyyy");
var isSaturday = (date.getDay() == 6);

One better than that, datejs has some extensions which can make this a one liner:

var isSaturday = Date.parseExact("8/8/2012", "dd/MM/yyyy").is().saturday();

Upvotes: 5

freefaller
freefaller

Reputation: 19953

I can recommend the Open Source Datejs library for general date handling.

Using the library...

var dt = Date.parse(yourDateVariable);
if(Date.today().is().saturday()){
   ...
}

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try:

var myDate = new Date("08/08/2012");
console.log(myDate.getDay()); // if 0 then its Sunday

Upvotes: 1

Related Questions