www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 03-20-2007, 11:41 PM
    sylph sylph is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 7
    Javascript date comparison

    Hi,

    I have a form for user to apply for booking of place. The date of event (from text field) should be at least 7 days from the date of application. In other words, the user has to apply at least 7 days before the date of the event.

    here is the javascript

    //if the event date of the text field is not empty
    if(c.txtDate.value !="")
    {
    var myDate = c.txtDate.value;
    var myDate_array = myDate.split("-");
    var dd = myDate_array[0];
    var mth = myDate_array[1];
    var yy = myDate_array[2];

    var argumentDate = new Date(yy, mth, dd);
    //get the date 7 days before the event
    argumentDate.setDate(argumentDate.getDate()-7);

    var day = argumentDate.getDate();
    var month = argumentDate.getMonth();
    var year = argumentDate.getYear();
    if (day<10) {
    day="0"+day;
    }
    if (month<10) {
    month="0"+month;
    }
    var limitDate = day + "-" + month + "-" + year;
    //get today's date to compare.
    var today = new Date();
    var d = today.getDate();
    var m = today.getMonth();
    var y = today.getYear();
    var todayDate = d + "-" + m + "-" + y;
    if (todayDate>limitDate) {
    alert ("The application date has to be at least 7 days before the event date");
    return false;
    }
    }

    It validates correctly if the date entered is within this year. but if the user were to key in 2006, the validation return true. I have tried changing the date format to year-month-day, but the validation doesn't work as it return false even it is more than 7 days. How can I validate it?
    Thanks ! =)
    Reply With Quote
      #2  
    Old 03-21-2007, 01:15 AM
    CrazyMerlin's Avatar
    CrazyMerlin CrazyMerlin is offline
    Helper!
     
    Join Date: Dec 2005
    Location: Queen Creek, AZ
    Posts: 1,568
    convert both dates to timestamps and compare them that way....it is much quicker and perfectly simple.
    __________________
    Design first! Code later!
    Reply With Quote
      #3  
    Old 03-21-2007, 01:21 AM
    sylph sylph is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 7
    Thanks for your reply
    I can actually convert the value in the text field to timestamp???
    Reply With Quote
      #4  
    Old 03-21-2007, 02:04 AM
    CrazyMerlin's Avatar
    CrazyMerlin CrazyMerlin is offline
    Helper!
     
    Join Date: Dec 2005
    Location: Queen Creek, AZ
    Posts: 1,568
    yes, simply create a date object from the date string, then get dateObject.getTime()

    where dateObject is the name of your date object
    __________________
    Design first! Code later!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 04:16 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.