Reputation: 47945
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
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
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
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
Reputation: 100175
Try:
var myDate = new Date("08/08/2012");
console.log(myDate.getDay()); // if 0 then its Sunday
Upvotes: 1