Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-04-2006, 10:20 PM   PM User | #1
parallon
Regular Coder

 
Join Date: May 2005
Posts: 215
Thanks: 14
Thanked 0 Times in 0 Posts
parallon is an unknown quantity at this point
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
parallon is offline   Reply With Quote
Old 08-05-2006, 07:33 AM   PM User | #2
mic2100
Regular Coder

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

Hope this helps!
mic2100 is offline   Reply With Quote
Old 08-05-2006, 10:40 AM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,379
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
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.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 08-06-2006, 02:48 AM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 710
Thanks: 31
Thanked 128 Times in 119 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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..
mrhoo is offline   Reply With Quote
Old 08-07-2006, 05:06 PM   PM User | #5
parallon
Regular Coder

 
Join Date: May 2005
Posts: 215
Thanks: 14
Thanked 0 Times in 0 Posts
parallon is an unknown quantity at this point
mrhoo:

Thank you. That worked great.

Parallon
parallon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 AM.


Advertisement
Log in to turn off these ads.