Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Subtracting days from the current date using Javascript


Message #1 by anne.do@c... on Thu, 21 Nov 2002 14:04:47
Thanks! That worked.  I'm taking a javascript class soon so I'll know better
when to use = versus ==.  Thanks again

Anne 

-----Original Message-----
From: sara [mailto:fillet70@h...] 
Sent: Thursday, November 21, 2002 10:53 AM
To: javascript
Subject: [javascript] Re: Subtracting days from the current date using
Javascript

Here is the solution. What you did did not work because the value in the 
days field is a text field. You have check its value before you use the 
parseInt function to convert it to a number.


<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
function MakeDate()
{
//Calculation of a day in milliseconds. 
var oneday = 60 * 1000 * 60 * 24 ;
var days = 
document.searchform.Days.options
[document.searchform.Days.selectedIndex].value

if (days == "ALL")
{
document.searchform.startdate.value = "";
}
else
{
var daysback= 
parseInt(days)

//	var startdate = (I want startdate=today's date-daysback)
var currdate = new Date()
var currdatems = currdate.getTime()

var diffms = oneday * daysback

var daysbackdate = new Date()
daysbackdate.setTime(currdatems - diffms)

var startdate = (daysbackdate.getMonth() + 1) + "/" + daysbackdate.getDate
() + "/" + daysbackdate.getFullYear()

document.searchform.startdate.value = startdate;
}

}

//-->
</SCRIPT>

<form name="searchform">
<select name="Days" size=1 onChange="MakeDate()">
                <option value="10">10 </option>
                <option value="20">20</option>
                <option value="30">30 </option>
		<option value="ALL"> Search all dates </option>
 </select>
              
<input name="startdate" type="text" size="10">

</form>

</BODY>
</html>








> I have a form that I want to be dyanmic.  When the drop down select box 
n> ame days is selected I want Javascript to run and subtract the number 
of 
d> ays selected from today's date.  Below is what I have so far.  Please 
a> dvise on how I can do the date subtraction:

> <SCRIPT LANGUAGE="JavaScript">
<> !--
f> unction MakeDate()
{> 
v> ar daysback= 
d> 
ocument.SearchForm.Days.optionsdocument.SearchForm.Days.selectedIndex].val
u> e
	> var startdate = (I want startdate=today's date-daysback)
	> document.SearchForm.startdate.value = startdate;
}> 

> //-->
<> /SCRIPT>

> <form name="searchform">
<> select name="Days" size=1 onChange="MakeDate()">
 >                <option value="10">10 </option>
 >                <option value="20">20</option>
 >                <option value="30">30 </option>
 > </select>
 >              
<> input name="startdate" type="text" size="10">

> </form>

  Return to Index