Date compare

Discussion in 'JavaScript' started by pero2000, Feb 25, 2007.

  1. #1
    I'm working on a page with two dates displayed in
    two textfialds (startDate and endDate).
    User enters them from js calendar. After that, and before
    db search, dates have to be validated -

    startDate > today
    endDate > startDate
    startDate <> endDate

    I written function as below:

    function search()
    {
    valid = true;
    var startdate = new Date(form.datum1.value)
    var enddate = new Date(form.datum2.value)
    var today = new Date ()


    if (today > startdate)
    {
    alert ('Please, enter correct "Date from" !');
    datum1.focus();
    return false;
    }

    if (startdate == enddate)
    {
    alert ('Please, enter correct "Date to" !');
    datum2.focus();
    return false;
    }

    if (startdate > enddate)
    {
    alert ('Please, enter correct "Date to" !');
    datum2.focus();
    return false;
    }

    else
    form.action = "rezultati-rez.asp"
    method = "POST"
    form.submit()

    }

    It work OK only for third case, and not for other two. Does anybody have solution ?
     
    pero2000, Feb 25, 2007 IP
  2. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I don't think you can create Date object like that. You need to first create the date object and then set the date. For example
    
    var myDate=new Date()
    myDate.setFullYear(2010, 0, 14)
    var today = new Date()
    if (myDate>today)
      alert("Today is before 14th January 2010")
    else
      alert("Today is after 14th January 2010")
    
    Code (markup):
    Also why are you checking (startdate >= enddate) and (startdate > enddate) separately when you can have the same result by
    using (startdate >= enddate)
     
    Aragorn, Feb 27, 2007 IP