...

View Full Version : Date Comparison



brendon99
01-15-2004, 10:34 PM
Hi,

Within a function, I'm trying to find out if the parameter date is greater than another date.

Can you do a comparison between dates in Jscript , or do you have to parse it beforehand into year, month and day?

Currently my function receives an input parameter (which is a date loaded from an XML file "1/27/2004"). I tried to parse it as a date
ie. var my_converted_date = new Date(parameter_date)
var my_converted_date_year = my_converted_date.getFullYear()
var my_converted_date_month = my_converted_date.getMonth()+1
var my_converted_date_day = my_converted_date.getDate()


However it crashes at the 1st line with "Wrong number of arguments or invalid property assisgnment"

justame
01-16-2004, 12:03 AM
bre...
does this just a help® you???
/me has it just a copied/credited/saved® from jyoung (Monday, May 05, 2003, 6:29:12 PM)...

/me just a knows® its nottt exactly whattt youre looking for 'cept mayyybe somewhere in the coding??? youll find some help...:O)))



<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>

brendon99
01-16-2004, 04:55 AM
thanks j,

but unfortunately I can't seem to parse the date to get the day/month and year from it. The strange thing is that if I define the date within the function (ie. var parameter_date = '1/27/2004'), then it parses it fine, and will calculate correctly.

If however the date is passed to the function, it won't work, even though its exactly the same. The function call is made from within an event in an HTML object, whilst the value is actually passed using XSLT.

ie.
<xsl:variable name="name_color" select="order:get_color(@lv)"/>

brendon99
01-16-2004, 06:34 AM
Finally, a breakthrough...

Discovered that I had to convert the XML value to a string, before passing it to the function. Then I could parse it correctly

ie. <xsl:variable name="name_color" select="order:get_color(string(@lv))"/>

All smiles now..... :)

Willy Duitt
01-16-2004, 06:56 AM
Did you try splitting the slashes from the date string
and then rebuild the array into a new Date string?

Something like this?

<script type="text/javascript">
<!--//
function chkDate(form){
var monthArry = new Array('January','February','March','April','May',
+'June','July','August','September','October','November','December');

var paraDate = '1/27/2004'.split('/');
var paraWhich = paraDate[0]-1;
var paraMonth = monthArry[paraWhich];
var paraDay = paraDate[1];
var paraYear = paraDate[2];
var paraStr = paraMonth+' '+paraDay+' '+paraYear;

var formWhich = form.myMonth.value-1;
var formMonth = monthArry[formWhich];
var formDay = form.myDay.value;
var formYear = form.myYear.value;
var formStr = formMonth+' '+formDay+' '+formYear;

if(new Date(formStr)<new Date(paraStr))
alert("Valid Date");
else
alert("Not a Valid Date");
}
//-->
</script>
</HEAD>

<BODY>
<form name="myDate">
mm<input type="text" name="myMonth" size="1">
dd<input type="text" name="myDay" size="1">
yyyy<input type="text" name="myYear" size="2">
<input type="button" onclick="chkDate(this.form)" value="check">
</form>

.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum