Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    215
    Thanks
    14
    Thanked 0 Times in 0 Posts

    trouble comparing dates

    Hello all. I am pulling dates from a text field and need to compare them to make sure that the To date is >= than the From date. Here is the function that I have, although it seems to be looking at it as a number rather than a date. In other words, 8/2/2006 is showing up as larger than 8/11/2006 because 2 is greater than 1. Here is the function:

    PHP Code:
      if (frm.txtTargetDate.value.length && frm.txtTargetEnd.value.length) {
          if (
    frm.txtTargetDate.value frm.txtTargetEnd.value) {
        
    //Message:"Please make sure that your End Date is Greater than or Equal to your Start Date."
        
    alert("Please make sure that your \"End Date\" is \n\nGreater than or Equal to your \"Start Date\".");
        
    frm.txtTargetEnd.focus()
        return
          }
      } 
    Is there something I need to do to the numbers first?

    Thanks,

    parallon

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    yeah 11 is less that 2 so if u make it into a 02 then it will come before 11

    Hope this helps!

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    select lists are mm better than textboxes but

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    var DateFormat='DD/MM/YYYYY';
    
    function CkDates(){
     var frm=document.fred;
     var d1=frm['D1'];
     var d1v=DateCk(d1.value);
     if (!d1v){ alert('Incorrect Date Format1'); d1.focus(); return false; }
     var d2=frm['D2'];
     var d2v=DateCk(d2.value);
     if (!d2v){ alert('Incorrect Date Format1'); d2.focus(); return false}
     if (d1v>=d2v){ alert('Date 2 must be greater than Date 1'); return false; }
     return true
    }
    
    function DateCk(val){
     if (val.length!=9){ return false; }
     var val=val.split(DateFormat.charAt(2));
     for (var zxc0=0;zxc0<val.length;zxc0++){
      if (isNaN(val[zxc0])){  return false; }
     }
     if (val.length!=3){ return false; }
     if (DateFormat.charAt(0)=='D'){
      if (val[1]>12||val[1]<1){  return false; }
      if (val[0]>DaysinMonth(val[2],val[1])||val[0]<1){ return false; }
      val=''+val[2]+FormatNu(val[1])+FormatNu(val[0]);
     }
     else {
      if (val[0]>12||val[0]<1){ return false; }
      if (val[1]>DaysinMonth(val[2],val[0])||val[1]<1){return false; }
      val=''+val[2]+FormatNu(val[0])+FormatNu(val[1]);
     }
     return val;
    }
    
    function FormatNu(nu){
     return (nu<10)?'0'+nu:nu;
    }
    
    function DaysinMonth(year,month){
      return new Date(parseInt(year),parseInt(month),0).getDate();
    }
    /*]]>*/
    </script></head>
    
    <body>
    <form name="fred" >
    <input name="D1" value="10/9/2006" />
    <input name="D2" value="11/9/2006"  />
    <input type="button" value="Ck Dates" onclick="CkDates();" >
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Convert the strings to Date objects before you subtract:
    (validate user input first or use a try/catch as here):
    Code:
    try{
        if(new Date(txtTargetEnd.value)-new Date(txtTargetDate.value) >0) {
        //  do something if the end is later than the beginning
        }
        //   else targetEnd was before targetDate;
    }
    catch(er){
        // one or both of the Date strings won't make a Date- 
        // focus on the errant field and alert user to retype the date 
        // (or suggest a possible correction as the input element's value)
    }
    Last edited by mrhoo; 08-06-2006 at 02:50 AM.

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    215
    Thanks
    14
    Thanked 0 Times in 0 Posts
    mrhoo:

    Thank you. That worked great.

    Parallon


  •  

    Posting Permissions

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