View Full Version : trouble comparing dates

08-04-2006, 11:20 PM
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:

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\".");

Is there something I need to do to the numbers first?



08-05-2006, 08:33 AM
yeah 11 is less that 2 so if u make it into a 02 then it will come before 11

Hope this helps!

08-05-2006, 11:40 AM
select lists are mm better than textboxes but

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<script language="JavaScript" type="text/javascript">

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; }
else {
if (val[0]>12||val[0]<1){ return false; }
if (val[1]>DaysinMonth(val[2],val[0])||val[1]<1){return false; }
return val;

function FormatNu(nu){
return (nu<10)?'0'+nu:nu;

function DaysinMonth(year,month){
return new Date(parseInt(year),parseInt(month),0).getDate();

<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();" >


08-06-2006, 03:48 AM
Convert the strings to Date objects before you subtract:
(validate user input first or use a try/catch as here):

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;
// 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)

08-07-2006, 06:06 PM

Thank you. That worked great.