SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    comparing dates...what am I doing wrong?

    hi all,
    I have a form where the user clicks on a little calendar image and a calendar pops up...they select the date and it gets placed into the field.

    then, i need to make sure that they have not selected a date AFTER today's date...so here is the code I used:

    var today = new Date();
    today = (padout(today.getMonth()+1)) + "/" + padout(today.getDate()) + "/" + padout(today.getFullYear());
    if (document.claims.DateofLoss.value > today)
    {
    alert ("Please select a date before today's date!");
    return false;
    }

    This partially works...if I select any date in 2005 that is after today's date, it gives me the message and works...if I select any date in 2005 before today's date, it works fine and lets me through...

    HOWEVER, if I select a date BEFORE today's date but in 2004 or before, it gives me the message again that "Please select a date before today's date!"....!?!? why?

    please help! Thank you.

  2. #2
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try creating functions that test the input year etc to the current then if all of those return true then let them through otherwise alert.

    ex

    function checkYear(form, today){
    if(form < today)
    return true
    }

    // each funtion checks to see if each is less than the other and returns a boolean
    yearOK = checkYear(formYear, todaysYear);
    monthOK = checkMonth(formMonth, todaysMonth);
    dayOK = checkDay(formDay, todaysDay);

    if(yearOK)
    return 0;
    if(!yearOK)
    if(formYear == todayYear)
    if(MonthOK)
    return 0;
    else
    alert();
    ...
    }
    ...
    }
    else{
    alert();
    }

    these functions dont work in the correct manor but they give you an idea of another method of checking. more round about but it works as long as your logic is ok.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to construct a Date object from user input using parse or setXXX methods and then compare it (numeric) to today's date.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey stereofrog,
    im trying to implement wat you said...it makes sense...turn the date into a number (milliseconds from epoch) and then compare numbers....right?

    i cant seem to figure it out though! i've been trying to use the date.parse function but can't seem to make it work.

    can u give me any code help?

    thanks!

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind, i got it.....here is wat i used:

    var difference = 0;
    inputdate = new Date(document.claims.DateofLoss.value);
    today = new Date();
    difference = today - inputdate;
    if (difference < 0)
    {
    alert ("Please select a date before today's date!");
    return false;
    }

    it worked....please let me know if anyone sees a problem with this that i am not seeing.

    thanks again for help!


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
  •