SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast lkagan's Avatar
    Join Date
    Sep 2007
    Location
    Boca Raton, Florida
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setFullYear() issue

    In the code below, setFullYear() is not working as expected. The date object remains today's date regardless of what values I supply.

    I've confirmed that the values passed to the method are correct and valid. It appears that something outside of that line is affecting it. I've also copied the code and pasted it in it's own document and it ran successfully. Anyone have any ideas?

    I found a workaround by using setMonth() & setDate() but I would still like to understand what's going on here.

    Thanks in advance.

    Code JavaScript:
     
    /**
    * Ensure proper dates are supplied if any.
    *
    * @return bool
    */
    function checkDates()
    {
        var startDate = document    
        var whiteSpace = /^[ \t\s]+|[ \t\s]+$/;
        var startDate = document.getElementById('startDate').value.replace(whiteSpace, '');
        var endDate = document.getElementById('endDate').value.replace(whiteSpace, '');
        var dateRangeErr = 'Invalid date range supplied.';
        var startDateFormatErr = 'Invalid start date format supplied.';
        var endDateFormatErr = 'Invalid end date format supplied.';
     
        // If the user supplied no dates, it's cool.
        if(startDate == '' && endDate == '') {
            return true;
        }
     
        // Check to make sure that either both dates or no dates were supplied.
        if((startDate == '' && endDate != '') || startDate != '' && endDate == '') {
            alert(dateRangeErr);
            return false;
        }
     
        // Check for valid formatting of the dates.
        var dateRegex = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        if(startDate.search(dateRegex) == -1) {
            alert(startDateFormatErr);
            return false;
        }
     
        if(endDate.search(dateRegex) == -1) {
            alert(endDateFormatErr);
            return false;
        }
     
        // Ensure that the start date is the same as or prior to the end date.
        var startDateParts = startDate.split('/');
        var endDateParts = endDate.split('/');
        var startDateObj = new Date();
        var endDateObj = new Date();
     
        startDateObj.setFullYear(startDateParts[2], startDateParts[0] - 1, startDateParts[1]);
        endDateObj.setFullYear(endDateParts[2], endDateParts[0] - 1, endDateParts[1]);
     
        if(endDateObj < startDateObj) {
            alert(dateRangeErr);
            return false;
        }
     
        return true;
    }
    Larry Kagan
    Lead Web Application Developer
    Superiocity, Inc.

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,332
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    I also pasted the code into a document and it works as expected.
    Which "date object remains today's date"?
    What other code is interacting with this page?
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Enthusiast lkagan's Avatar
    Join Date
    Sep 2007
    Location
    Boca Raton, Florida
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    I also pasted the code into a document and it works as expected.
    Which "date object remains today's date"?
    Both objects behave the same by staying populated with the current date.

    Quote Originally Posted by ParkinT View Post
    What other code is interacting with this page?
    There's a bout 300 inline lines of JS code on this page. I also have 3 included JS files that include hundreds more lines.
    Larry Kagan
    Lead Web Application Developer
    Superiocity, Inc.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •