PDA

View Full Version : Difference between two dates


Gary Williams
03-24-2003, 09:07 AM
Hi All,

I'm trying to check if a date entered as text in the format dd/mm/yyyy is in the future (good!) or in the past (bad!).

var date = quotation.coverdate.value;
var today = new Date();
if (Date.parse(date)>Date.parse(today)); {
alert('Cover date is recorded as '+date+'');
alert('Todays date is recorded as '+today+'');
alert("Cover date is OK.");
}
else {
alert("Please choose a date from tomorrow onwards.");
quotation.coverdate.focus();
return false;
}

I added the first two alerts to see what is going on, but I still can't fix it.

Please help.

Regards

Gary

justame
03-24-2003, 01:09 PM
gar...
/me has thisss just a copied/credited/saved® from jyoung...
mayyybe it will just a help® ya???


<html>
<head>
<title>Date Form Test</title>
<SCRIPT LANGUAGE="JavaScript">
// This sample script written by Jim Young

function chkdate()
{
bmonthidx=document.myform.month.selectedIndex
bdayidx=document.myform.day.selectedIndex
byearidx=document.myform.year.selectedIndex
if(bmonthidx==0||bdayidx==0||byearidx==0)
{
alert("You must select a value for all date fields")
}
else
{
var bmonth=document.myform.month.options[bmonthidx].value
var bday=document.myform.day.options[bdayidx].value
var byear=document.myform.year.options[byearidx].value
// join year month day together
var bdate=" "+byear+bmonth+bday;
// Get current date
curdate=new Date()
curmonth=curdate.getMonth()+1
curday=curdate.getDate()
curyear=curdate.getFullYear()
if(curmonth<10)
{
curmonth="0"+curmonth
}
if(curday<10)
{
curday="0"+curday
}
cdate=" "+curyear+curmonth+curday
// compare dates
if(bdate>=cdate)
{
alert("Date cannot be greater than or equal to Current date")
document.myform.month.focus()
}
else
{
alert("Date Correct")
}
}
}
function checkdate()
{
begmid=document.myform.month.selectedIndex
begdid=document.myform.day.selectedIndex
begyid=document.myform.year.selectedIndex
if(begmid==0||begdid==0)
{
alert("You must select a month and day")
document.myform.month.focus()
return false
}
else
{
if((begmid==4 || begmid==6 || begmid==9 || begmid == 11) && (begdid > 30))
{
alert("Day cannot be greater than 30")
document.myform.day.focus()
}
else
{
if(begmid==2)
{
numyear=parseFloat(document.myform.year.options[begyid].value)
remain=numyear % 4 // check for leap year
if(remain!=0)
{
if(begdid>28)
{
alert("The day is invalid")
document.myform.day.focus()
}
}
else
{
if(begdid>29)
{
alert("The day is invalid")
document.myform.day.focus()
}
}
}
}
}
}
function begother()
{
begmid=document.myform.month.selectedIndex
begdid=document.myform.day.selectedIndex
begyid=document.myform.year.selectedIndex
if(begmid>0 && begdid>0 && begyid>0)
{
checkdate()
}
}
</SCRIPT>
</head>
<body bgcolor="lightgreen"
onLoad="document.myform.month.focus(),document.myform.month.select()">
<CENTER>
<FORM NAME="myform">
Select Date: <SELECT NAME="month" onChange="begother()">
<OPTION VALUE=" "SELECTED> </OPTION>
<OPTION VALUE="01">01</OPTION>
<OPTION VALUE="02">02</OPTION>
<OPTION VALUE="03">03</OPTION>
<OPTION VALUE="04">04</OPTION>
<OPTION VALUE="05">05</OPTION>
<OPTION VALUE="06">06</OPTION>
<OPTION VALUE="07">07</OPTION>
<OPTION VALUE="08">08</OPTION>
<OPTION VALUE="09">09</OPTION>
<OPTION VALUE="10">10</OPTION>
<OPTION VALUE="11">11</OPTION>
<OPTION VALUE="12">12</OPTION>
</SELECT>
/
<SELECT NAME="day" onChange="begother()">
<OPTION VALUE=" "SELECTED> </OPTION>
<OPTION VALUE="01">01</OPTION>
<OPTION VALUE="02">02</OPTION>
<OPTION VALUE="03">03</OPTION>
<OPTION VALUE="04">04</OPTION>
<OPTION VALUE="05">05</OPTION>
<OPTION VALUE="06">06</OPTION>
<OPTION VALUE="07">07</OPTION>
<OPTION VALUE="08">08</OPTION>
<OPTION VALUE="09">09</OPTION>
<OPTION VALUE="10">10</OPTION>
<OPTION VALUE="11">11</OPTION>
<OPTION VALUE="12">12</OPTION>
<OPTION VALUE="13">13</OPTION>
<OPTION VALUE="14">14</OPTION>
<OPTION VALUE="15">15</OPTION>
<OPTION VALUE="16">16</OPTION>
<OPTION VALUE="17">17</OPTION>
<OPTION VALUE="18">18</OPTION>
<OPTION VALUE="19">19</OPTION>
<OPTION VALUE="20">20</OPTION>
<OPTION VALUE="21">21</OPTION>
<OPTION VALUE="22">22</OPTION>
<OPTION VALUE="23">23</OPTION>
<OPTION VALUE="24">24</OPTION>
<OPTION VALUE="25">25</OPTION>
<OPTION VALUE="26">26</OPTION>
<OPTION VALUE="27">27</OPTION>
<OPTION VALUE="28">28</OPTION>
<OPTION VALUE="29">29</OPTION>
<OPTION VALUE="30">30</OPTION>
<OPTION VALUE="31">31</OPTION>
</SELECT>
/
<SELECT NAME="year" onChange="checkdate()">
<OPTION VALUE=" "SELECTED> </OPTION>
<OPTION VALUE="1980">1980</OPTION>
<OPTION VALUE="1981">1981</OPTION>
<OPTION VALUE="1982">1982</OPTION>
<OPTION VALUE="1983">1983</OPTION>
<OPTION VALUE="1984">1984</OPTION>
<OPTION VALUE="1985">1985</OPTION>
<OPTION VALUE="1986">1986</OPTION>
<OPTION VALUE="1987">1987</OPTION>
<OPTION VALUE="1988">1988</OPTION>
<OPTION VALUE="1989">1989</OPTION>
<OPTION VALUE="1990">1990</OPTION>
<OPTION VALUE="1991">1991</OPTION>
<OPTION VALUE="1992">1992</OPTION>
<OPTION VALUE="1993">1993</OPTION>
<OPTION VALUE="1994">1994</OPTION>
<OPTION VALUE="1995">1995</OPTION>
<OPTION VALUE="1996">1996</OPTION>
<OPTION VALUE="1997">1997</OPTION>
<OPTION VALUE="1998">1998</OPTION>
<OPTION VALUE="1999">1999</OPTION>
<OPTION VALUE="2000">2000</OPTION>
<OPTION VALUE="2001">2001</OPTION>
</SELECT>
<BR>
<INPUT TYPE="button" NAME="but1" VALUE="Check Date Range" onClick="chkdate()">
<BR><BR>
<FONT SIZE="-1" COLOR="red">Date format is: mm/dd/yyyy.</FONT>
</FORM>
</CENTER>
</body>
</html>


/me just a saw® this line in thattt coding up there...
Date cannot be greater than or equal to Current date

sooo mayyybe ya can just a mess® with thattt line???

just a goodluck® n' hoping it helps til the pros come just a long® lol...