Forum Moderators: open

Message Too Old, No Replies

Calculating day between 2 dates

javascript help

         

smokeyb

7:55 pm on May 3, 2004 (gmt 0)

10+ Year Member



I wonder if someone would be kind enough to help me with this issue. I need to calculate the number of days between to dates (precisely) and I found a little tute on how to do it with javascript. Problem is, although it gives you the code it doesn't tell you how to incorporate it into a page or form, and I am JS illiterate. Here's the code:
<script type="text/javascript">

//Set the two dates
var startingdate=new Date(1997, 5, 11) //Month is 0-11 in JavaScript
var today=new Date()
//Get 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
document.write(Math.ceil((today.getTime()-startingdate.getTime())/(one_day))+
" days has gone by since widgets.com started!")

</script>


Can someone elaborate on how to set input fields and call the script?
This is something I need personally to calculate "interest" and it will be a standalone and not online.
Thanks in advance
Smokey

birdbrain

9:03 pm on May 4, 2004 (gmt 0)



Hi there smokeyb,

I have modified the script slightly.
Just hope that this is what you had invisaged ;)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>from today</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
background:#cccccc;
}
input {
width:40px;
margin:3px;
}
#sbmt,#rst {
width:62px;
}
div#cntnt {
position:absolute;
top:20%; /*adjust this to position - y*/
left:40%; /*adjust this to position - x*/
background:#ffffff;
padding:10px;
border:double 6px #000000;
width:160px;
font-family:arial;
font-size:14px;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[
function daysTo(year,month,day){
//Check for numbers only
if(isNaN(document.forms[0][0].value) ¦¦ isNaN(document.forms[0][1].value) ¦¦ isNaN(document.forms[0][2].value)){
alert("numbers only thankyou!");return;
}
//Set the two dates
var startingdate=new Date(year,month-1,day); //Month is 0-11 in JavaScript
var today=new Date();

//Get 1 day in milliseconds
var one_day=1000*60*60*24;

//Calculate difference btw the two dates, and convert to days
document.forms[0][3].value=-Math.floor((today.getTime()-startingdate.getTime())/(one_day));

}
//]]>
</script>

</head>
<body>

<div id="cntnt">From today's date
<form action="">

<div>
<input type="text"/> : year <br />
<input type="text"/> : month <br />
<input type="text"/> : day <br />
<input type="text"/> : no. of days <br />
<input id="sbmt" type="submit" value="submit"
onclick="daysTo(document.forms[0][0].value,document.forms[0][1].value,document.forms[0][2].value);return false"/>
<input id="rst" type="reset"/>
</div>

</form>
</div>

</body>
</html>

Unfortunately this forum does not render code accurately.
When you C & P this code to test it you will have to amend this line...

if(isNaN(document.forms[0][0].value)¦¦isNaN(document.forms[0][1].value)¦¦isNaN(document.forms[0][2].value)){ 

Those double broken vertical lines are not what I typed and
must be replaced with the two solid vertical lines. You will find the key to the left of the Z ;)

birdbrain

smokeyb

10:24 pm on May 4, 2004 (gmt 0)

10+ Year Member



WOW! Thanks birdbrain, I can't imagine how much work that took. Thanks also for the clarity of your reply, I did it in a minute with notepad and it worked first time. The only adjustment I may like to find out how to do is: having another input field for the later date, instead of the current day (I figured that the code I submitted calculated it to the present day, from the limited knowledge I have of JS). I would prefer not to have to do this chore on the day that is calculated to, then I could do a few all at once.
I don't want to seem ungratelful but any ideas?
Smokey

birdbrain

10:33 am on May 5, 2004 (gmt 0)



Hi there smokeyb,

No problem ;)

This will calculate the number of days between two dates.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>from today</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
background:#cccccc;
}
input {
width:60px;
margin:3px;

font-family:arial;
font-size:14px;
}
#sbmt,#rst {
width:62px;
}
div#cntnt {
position:absolute;
top:20%; /*adjust this to position - y*/
left:40%; /*adjust this to position - x*/
background:#ffffff;
padding:10px;
border:double 6px #000000;
width:240px;
font-family:arial;
font-size:14px;
}
/*//]]>*/
</style>
<script type="text/javascript">
//<![CDATA[
function daysTo(year,month,day,year1,month1,day1){
for(i=0;i<6;i++){
if(isNaN(document.forms[0][2+i].value)){
alert("numbers only thankyou!");
return;
}
}
var startingdate=new Date(year,month-1,day);
var finishingdate=new Date(year1,month1-1,day1);
var today=new Date();
var one_day=1000*60*60*24;

document.forms[0][8].value=Math.floor((today.getTime()-startingdate.getTime()+(finishingdate.getTime()-today.getTime()))/(one_day));
}
//]]>
</script>
</head>
<body>

<div id="cntnt">
<form action="">
<table><tr>
<td><input type="text" readonly="readonly"value="1st date"/></td><td><input type="text" readonly="readonly"value="2nd date"/></td><td>&#160;</td></tr><tr>
<td><input type="text"/></td><td><input type="text"/></td><td> : year </td></tr><tr>
<td><input type="text"/></td><td><input type="text"/></td><td> : month </td></tr><tr>
<td><input type="text"/></td><td><input type="text"/> </td><td>: day </td></tr><tr>
<td>&#160;</td><td><input type="text"/></td><td>: no. of days </td></tr><tr>
<td><input id="sbmt" type="submit" value="submit"
onclick="daysTo(document.forms[0][2].value,document.forms[0][4].value,document.forms[0][6].value,
document.forms[0][3].value,document.forms[0][5].value,document.forms[0][7].value);return false"/>
</td><td><input id="rst" type="reset"/></td><td>&#160;</td>
</tr></table>
</form>
</div>

</body>
</html>

I hope that this helps ;)

birdbrain

smokeyb

10:43 am on May 5, 2004 (gmt 0)

10+ Year Member



Eternally in your debt Birdbrain. I'm a London boy too, so if you ever need a favour? you only have to ask.
Thanks again
Adrian aka Smokey B