Connecting Tech Pros Worldwide Help | Site Map

Checking if date entered is a weekend

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 2 Weeks Ago
Hi all, long time reader, first time poster :D

I've got a page i've put together in javascript, and one of the fields takes the date (in dd/mm/yyyy) format.

What I'm trying to do, is if a user enters a date which falls on a weekend (eg, 07/11/2009), then to throw an error.

I've had a play, but i'm very confused and couldn't get anything to work :(

Fingers crossed someone can help :D

Thanks
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: 2 Weeks Ago

re: Checking if date entered is a weekend


Create a new Date object from the input and
use its getDay() or getUTCDay() method.

Saturday returns 6, Sunday is 0.

If d is a date object, d.getDay()%6==0 is true if the day is Saturday or Sunday.
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 2 Weeks Ago

re: Checking if date entered is a weekend


I'm using the following script from the site below to check if the date entered is valid first of all, can I be cheeky/a pain and ask where in the script would I add your check?

Thanks

http://javascript.internet.com/forms/validate-date.html
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,577
#4: 1 Week Ago

re: Checking if date entered is a weekend


Anywhere within the validation part of the function, though the function itself isn't the best out there. The function doesn't convert to the Date object. Try doing that. For the Date object, see https://developer.mozilla.org/en/Cor...e/Objects/Date.
Reply

Tags
checking, date, javascript, weekend