Checking if date entered is a weekend

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • welshboi
    New Member
    • Nov 2009
    • 2

    #1

    Checking if date entered is a weekend

    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
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    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.

    Comment

    • welshboi
      New Member
      • Nov 2009
      • 2

      #3
      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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.

        Comment

        • JaXXXoN
          New Member
          • Nov 2010
          • 1

          #5
          I'm fairly certain I remember seeing weekend and leapyear detection as a feature when I was checking out this library:



          It has a ton of great date related stuff and is the best tool I've found for formatting Date Objects.

          Comment

          Working...