Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can someone help me with a date comparison checkit is always false

Status
Not open for further replies.
Apr 22, 2008
9
US
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<html xmlns="<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
function validate_required4(){
var f = document.getElementById('Call_Date').value;
var d = new Date(f);
var x = (isNaN(d))
if (x==false){
var d = new Date();
d.setDate(d.getDate());
var formattedDate = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes();
var e = new Date(formattedDate);
var checkit = e > f;
alert(checkit);
}
if (x==true)
{alert("Please enter a valid date format mm/dd/yy");return false;}
}

</script>

<style type="text/css"></style>

</head>
<body>

<input type="text" id="Call_Date" name="Call_Date" />
<input type="button" value="validate date" onclick="validate_required4()" />

</body>
</html>
 
Here are 3 test results - they show what the internal values are for your variables and why they don't match


formattedDate =[3/23/2008 15:34]
e=[Sun Mar 23 15:34:00 EDT 2008]
f=[3/23/2008]

formattedDate =[3/23/2008 15:34]
e=[Sun Mar 23 15:34:00 EDT 2008]
f=[3/23/2008 15:34:00]

formattedDate =[3/23/2008 15:34]
e=[Sun Mar 23 15:34:00 EDT 2008]
f=[3/23/2008 15:34:00 EDT]


Hope this helps ...
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<html xmlns="<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
function validate_required4(){
var calldate = document.getElementById('Call_Date').value;
var newdate = new Date(calldate);
var isitavaliddate = (isNaN(newdate));
if (isitavaliddate==false){
var todaydate = new Date();
todaydate.setDate(todaydate.getDate());
var formattedDate = todaydate.getMonth() + '/' + todaydate.getDate() + '/' + todaydate.getFullYear() + ' ' + todaydate.getHours() + ':' + todaydate.getMinutes();
var checkit = (formattedDate > calldate);
alert(formattedDate);
alert(calldate);
alert(checkit);
}
if (x==true)
{alert("Please enter a valid date format mm/dd/yy");return false;}
}

</script>

<style type="text/css"></style>

</head>
<body>

<input type="text" id="Call_Date" name="Call_Date" />
<input type="button" value="validate date" onclick="validate_required4()" />

</body>
</html>
 
What are you actually trying to validate?

The method you are using will accept dates of 01/45/08 ( clearly not a date) as it will evaluate to 02/14/08 (which makes some sort of sense).

If you're looking to validate then you really need to validate the component parts mm, dd, and yy

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top