/    Sign up×
Community /Pin to ProfileBookmark

Show how many days difference?

Is it possible to show how many days difference 2 dates are within a table in a third <td> tag?

Here is an example:

<td>2005-12-15</td>
<td>2005-12-17</td>
<td>2</td>

I have a looped results page that will fill in the dates for me down the page. Also if the above is possible, can I output an average of all day differences at the bottom?

Thanks,
Aaron

to post a comment
JavaScript

76 Comments(s)

Copy linkTweet thisAlerts:
@UbikJan 06.2006 — [code=html]
<SCRIPT LANGUAGE="JavaScript"><!--
var dtDateBegin = new Date(2006, 00, 06);
var dtDateEnd = new Date(2005, 11, 25);
/* JS lameness = JSmonth= (realmonth-1) */

var intMsBegin = dtDateBegin.getTime();
var intMsEnd = dtDateEnd.getTime();

var intMsDiff = intMsEnd - intMsBegin;
document.write('days difference is ' + ' ' + intMsDiff/86400000 + '<br />');
/* 86400000 is the number of milliseconds in 1 day */
//--></SCRIPT>
[/code]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — Thanks but where do insert your code. I tried to insert it into the third <td> tag where the number shoud be shown but all I get is this.

"days difference is -12"

Any ideas?

Aaron
Copy linkTweet thisAlerts:
@CrazyMerlinJan 06.2006 — you need to wrap it in a function and pass it the two dates, then have it return intMsDiff/86400000 which will be the answer

he should have done that for you.
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — Whoa, you just blew my mind. I've got no clue what that means but it sounds hard. Is this easy for you to show me?

Thanks for helping,

Aaron
Copy linkTweet thisAlerts:
@CrazyMerlinJan 06.2006 — This shows you an example, you should be able to work it out:

[CODE]
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<SCRIPT LANGUAGE="JavaScript">
function showDiff(dFrom, dTo){
var dtDateBegin = new Date(dFrom);
var dtDateEnd = new Date(dTo);
var intMsBegin = dtDateBegin.getTime();
var intMsEnd = dtDateEnd.getTime();
var intMsDiff = intMsEnd - intMsBegin;

return intMsDiff/86400000;
}
</SCRIPT>
</head>

<body>

<table border="1" width="100%" id="table1">
<tr>
<td id="From">01-21-2005</td>
</tr>
<tr>
<td id="To">05-12-2005</td>
</tr>
<tr>
<td><script>document.write(showDiff(document.getElementById('From').innerText,document.getElementById('To').innerText))</script></td>
</tr>
</table>

</body>

</html>
[/CODE]


you can see that I had to give the TDs with the values an Id attribute, like a name.

then we take what is inside of the HTML and pass it to a function and write it into the 3rd cell
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Aaron, check your email.
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — CrazyMerlin thanks for helping me out but I get a javascript error with your example with no output.

aaron
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — [CODE]<html>
<head>
<script type="text/javascript">

function calcDays(){

var date1 = document.getElementById('d1').lastChild.data;
var date2 = document.getElementById('d2').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diffDays').lastChild.data = daysApart;
}

</script>
</head>
<body>
<table>
<tbody>
<td id='d1'>2005-12-15</td>
<td id='d2'>2005-12-17</td>
<td id='diffDays'>&nbsp</td>
</tbody>
</table>
<input type='button' value="Days Apart" onclick="calcDays()">
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — CrazyMerlin I did get yours to work, there was a space in "inner" however it doesn't round and is given me like 10 places after the decimal. Also I need to have my date in this format:

2005-12-01

Is this hard to change?

Thanks Ancora,

Your date format is exactly what I need but I can't use a button, it has to be outputted like the way CrazyMerlin has it.

If had had the 2 of your scripts combined I would be in bussiness. ?)

Any thoughts?

Aaron
Copy linkTweet thisAlerts:
@konithomimoJan 06.2006 — Here is ancora's code altered like Merlin's
&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

<i> </i>function calcDays(){

<i> </i> var date1 = document.getElementById('d1').lastChild.data;
<i> </i> var date2 = document.getElementById('d2').lastChild.data;
<i> </i> date1 = date1.split("-");
<i> </i> date2 = date2.split("-");
<i> </i> var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
<i> </i> var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
<i> </i> var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
<i> </i> document.getElementById('diffDays').lastChild.data = daysApart;
<i> </i>}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border="1"&gt;
&lt;tbody&gt;
&lt;td id='d1'&gt;2005-12-15&lt;/td&gt;
&lt;td id='d2'&gt;2005-12-17&lt;/td&gt;
&lt;td id='diffDays'&gt;&amp;nbsp&lt;/td&gt;
&lt;td&gt;&lt;script&gt;calcDays();&lt;/script&gt;&lt;/td&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;


or you could do it this way:&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
window.onload=calcDays;
function calcDays(){

<i> </i> var date1 = document.getElementById('d1').lastChild.data;
<i> </i> var date2 = document.getElementById('d2').lastChild.data;
<i> </i> date1 = date1.split("-");
<i> </i> date2 = date2.split("-");
<i> </i> var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
<i> </i> var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
<i> </i> var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
<i> </i> document.getElementById('diffDays').lastChild.data = daysApart;
<i> </i>}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border="1"&gt;
&lt;tbody&gt;
&lt;td id='d1'&gt;2005-12-15&lt;/td&gt;
&lt;td id='d2'&gt;2005-12-17&lt;/td&gt;
&lt;td id='diffDays'&gt;&amp;nbsp&lt;/td&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Aaron:

[CODE]<html>
<head>
<script type="text/javascript">

function calcDays(){

var date1 = document.getElementById('d1').lastChild.data;
var date2 = document.getElementById('d2').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diffDays').lastChild.data = daysApart;
}

onload=calcDays;

</script>
</head>
<body>
<table width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<td id='d1'>2005-12-15</td>
<td id='d2'>2005-12-17</td>
<td id='diffDays' align='middle'>&nbsp</td>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — Yes that is it. I will use the first example, however for my purpose it is not working with more than 1. I will need to repeat this code below a few times on one page:

<tr>

<td id='d1'>2005-12-15</td>

<td id='d2'>2005-12-20</td>

<td id='diffDays'>&nbsp</td>

<td><script>calcDays();</script></td>

</tr>


It is possible that I can use a unique ID# for each time it reapeats if needed.

Any ideas?

Aaron
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Aaron, placing script tags in the Body, in yet ANOTHER table cell is, well, silly. But, each to his own.
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — Ancora, your last code works great too but can it be modified to allow me to have the below code repeat multiple times like this?

<tr>

<td id='d1'>2005-12-15</td>

<td id='d2'>2005-12-17</td>

<td id='diffDays' align='middle'>&nbsp</td>

</tr>

<tr>

<td id='d1'>2005-12-20</td>

<td id='d2'>2005-12-27</td>

<td id='diffDays' align='middle'>&nbsp</td>

</tr>

Thanks,

Aaron
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — [CODE]<html>
<head>
<script type="text/javascript">

var nDateSets = 3;

function calcDays(){

for (i=1; i<=nDateSets; i++)
{
var date1 = document.getElementById('d'+i+'a').lastChild.data;
var date2 = document.getElementById('d'+i+'b').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diff'+i).lastChild.data = daysApart;
}
}

onload=calcDays;

</script>
</head>
<body>
<table width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<tr>
<td id='d1a'>2005-12-15</td>
<td id='d1b'>2005-12-17</td>
<td id='diff1' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d2a'>2005-12-20</td>
<td id='d2b'>2005-12-27</td>
<td id='diff2' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d3a'>2005-12-25</td>
<td id='d3b'>2005-12-31</td>
<td id='diff3' align='middle'>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Aaron, you don't need to assign an ID to the result fields. Try this:

[CODE]<html>
<head>
<script type="text/javascript">

var nDateSets = 3;

function calcDays(){

for (i=1; i<=nDateSets; i++)
{
var date1 = document.getElementById('d'+i+'a').lastChild.data;
var date2 = document.getElementById('d'+i+'b').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('d'+i+'b').nextSibling.lastChild.data = daysApart;
}
}

onload=calcDays;

</script>
</head>
<body>
<table width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<tr>
<td id='d1a'>2005-12-15</td>
<td id='d1b'>2005-12-17</td>
<td id='garbage9787878' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d2a'>2005-12-20</td>
<td id='d2b'>2005-12-27</td>
<td id='garbage983497849' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d3a'>2005-12-25</td>
<td id='d3b'>2005-12-31</td>
<td id='garbage0090909' align='middle'>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — I just added another <tr> set and it didn't output anything just a blank on that line. I added this with the other 3:

<tr>

<td id='d4a'>2005-12-6</td>

<td id='d4b'>2005-12-31</td>

<td id='garbage00909094' align='middle'>&nbsp</td>

</tr>


Does the "db4" for example have to increment up? Does anything here have to increment up appropriately?

Thanks,

Aaron
Copy linkTweet thisAlerts:
@Tweak4Jan 06.2006 — Yes. For the above script to work, each set of dates will have to be named dXa and dXb, where X is incremented for each pair (if you skip a number or deviate from this naming convention, the script will break).

Likewise, the "nDateSets" defined near the top will have to be set to the total number of pairs of start and end dates, since it controls the looping in the calculation
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — It's self-explanatory, here's FOUR sets of dates. Tell me you aren't helpless.

[CODE]<html>
<head>
<script type="text/javascript">

var nDateSets = 4;

function calcDays(){

for (i=1; i<=nDateSets; i++)
{
var date1 = document.getElementById('d'+i+'a').lastChild.data;
var date2 = document.getElementById('d'+i+'b').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('d'+i+'b').nextSibling.lastChild.data = daysApart;
}
}

onload=calcDays;

</script>
</head>
<body>
<table width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<tr>
<td id='d1a'>2005-12-15</td>
<td id='d1b'>2005-12-17</td>
<td id='garbage9787878' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d2a'>2005-12-20</td>
<td id='d2b'>2005-12-27</td>
<td id='garbage983497849' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d3a'>2005-12-25</td>
<td id='d3b'>2005-12-31</td>
<td id='garbage0090909' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d4a'>2005-12-25</td>
<td id='d4b'>2005-12-31</td>
<td id='garbage0090909' align='middle'>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — I see that, but I could have possibly 400 on a page. I can't increment up d3b d4b d5b and so on. Am I screwed on this one? This will all be dynamic in my search results so I won't be able to increment up on the id=dXX.

Thanks,

Aaron
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Aaron: If it's dynamic, you'll have to increment the id's for the date cells. What in the heck have you been looking at the last 3 hours?

But, regarding the number of rows, this works dynamically. Assign the id to the table.

End of the line for me. You're too demanding, and make no effort to work with the code on your own. None. Zero.

[CODE]<html>
<head>
<script type="text/javascript">

function calcDays(){

var nDateSets = document.getElementById('dateTbl').rows.length;
for (i=1; i<=nDateSets; i++)
{
var date1 = document.getElementById('d'+i+'a').lastChild.data;
var date2 = document.getElementById('d'+i+'b').lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('d'+i+'b').nextSibling.lastChild.data = daysApart;
}
}

onload=calcDays;

</script>
</head>
<body>
<table id='dateTbl' width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<tr>
<td id='d1a'>2005-12-15</td>
<td id='d1b'>2005-12-17</td>
<td id='garbage9787878' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d2a'>2005-12-20</td>
<td id='d2b'>2005-12-27</td>
<td id='garbage983497849' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d3a'>2005-12-25</td>
<td id='d3b'>2005-12-31</td>
<td id='garbage0090909' align='middle'>&nbsp</td>
</tr>
<tr>
<td id='d4a'>2005-12-25</td>
<td id='d4b'>2005-12-31</td>
<td id='garbage0090909' align='middle'>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 06.2006 — awe your too harsh on a rookie... I did work with your code but just could'nt get it right. You do good work but should'nt stress so much! You can still have the last laugh because I still haven't figured out how to "Assign the ID to the table"...
Copy linkTweet thisAlerts:
@AncoraJan 06.2006 — Right. I know. Take care, just the same. The "newbie" excuse, is, well, just that, an excuse. Stand up, and be an adult. Take responsibility for your projects. No excuses.

Then you can say to yourself, instead of me or someone else: "I do good work."
Copy linkTweet thisAlerts:
@CrazyMerlinJan 06.2006 — All you need do if you are going to have a ton of TDs with dates in is write a script that searches the inside of a td (innerText) and if it find a valid date, look for it's next sibling, get the date in that, do the calculation, then move to the next sibling and output the difference.

That way you need no id attributes at all, as everything is done recursively on the entire table, or infact on all tables on the page.
Copy linkTweet thisAlerts:
@adalbyauthorJan 07.2006 — Thank You!
Copy linkTweet thisAlerts:
@adalbyauthorJan 08.2006 — CrazyMerlin,

after playing around with your suggestion and Ancora coding I am still unable to get this to work with multiple(up to 400) rows on the same page. Is there another post that has similar coding that you've seen to help me out? Because I'm not javascript programmer I am having a difficult time with this. Sorry...

Thanks,

Aaron
Copy linkTweet thisAlerts:
@konithomimoJan 08.2006 — &lt;script type="text/javascript"&gt;
function calcDays(date1,date2)
{
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
return daysApart;
}


function doIt()
{
var cells = document.getElementsByTagName('td');
var i;
var day1, day2;

for(i=0;i&lt;cells.length;i++)
{
if((i%3) == 0)
{
day1 = cells[i].data;
day2 = cells[i+1].data;
cells[i+2].data = calcDays(day1,day2);
}
}
}

window.onload = doIt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorJan 08.2006 — thanks konithomimo will I need to add a body onload?
Copy linkTweet thisAlerts:
@konithomimoJan 08.2006 — No. I already added in:

window.onload = doIt;

to the end of the script. you can move it to a body onload though.
Copy linkTweet thisAlerts:
@adalbyauthorJan 08.2006 — Ok, I'm not getting a java error any more but its not calculating tha days can I use the same coding technique for my table or should I delete out all the "id" references?

<table id='dateTbl' width='220' border='1' cellspacing='0' cellpadding='5'>

<tbody>

<tr>

<td id='d1a'>2005-12-15</td>

<td id='d1b'>2005-12-17</td>

<td id='garbage9787878' align='middle'>&nbsp</td>

</tr>

<tr>

<td id='d2a'>2005-12-20</td>

<td id='d2b'>2005-12-27</td>

<td id='garbage983497849' align='middle'>&nbsp</td>

</tr>

<tr>

<td id='d3a'>2005-12-25</td>

<td id='d3b'>2005-12-31</td>

<td id='garbage0090909' align='middle'>&nbsp</td>

</tr>

<tr>

<td id='d4a'>2005-12-25</td>

<td id='d4b'>2005-12-31</td>

<td id='garbage0090909' align='middle'>&nbsp</td>

</tr>

</tbody>

</table>
Copy linkTweet thisAlerts:
@AncoraJan 08.2006 — [CODE]<html>
<head>
<script type="text/javascript">

function calcDays(){

nTable = document.getElementsByTagName('TABLE')[0];
nDateSets = nTable.rows.length-1;
for (i=0; i<=nDateSets; i++)
{
var date1 = nTable.rows[i].firstChild.lastChild.data;
var date2 = nTable.rows[i].firstChild.nextSibling.lastChild.data;
date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
nTable.rows[i].firstChild.nextSibling.nextSibling.lastChild.data = daysApart;
}
}

window.onload=calcDays;

</script>
</head>
<body>
<table width='220' border='1' cellspacing='0' cellpadding='5'>
<tbody>
<tr>
<td>2005-12-15</td>
<td>2005-12-17</td>
<td align='middle'>&nbsp</td>
</tr>
<tr>
<td>2005-12-20</td>
<td>2005-12-27</td>
<td align='middle'>&nbsp</td>
</tr>
<tr>
<td>2005-12-25</td>
<td>2005-12-31</td>
<td align='middle'>&nbsp</td>
</tr>
<tr>
<td>2005-12-25</td>
<td>2005-12-31</td>
<td align='middle'>&nbsp</td>
</tr>
</tbody>
</table>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@adalbyauthorJan 08.2006 — Thanks Ancora! This is EXACTLY what I am after!!! Thanks for all your help.

Aaron
Copy linkTweet thisAlerts:
@adalbyauthorJan 08.2006 — I have a database of about 400 hundred entries and not all the dates are entered in for the 2nd date. The way the script now handles the non date entry field with just a blank is fine but for the next row(entry) down and then for the rest of the rows no date differences are outputted to the screen even though they are present. Is it possible to continue the date differences after a missing date row? Also is it possible to use another <table> above this date difference table?


Aaron
Copy linkTweet thisAlerts:
@konithomimoJan 09.2006 — Is it possible to continue the date differences after a missing date row?[/QUOTE]

yes, just put in an if statement the checks to see if either date cells are empty. If not then do go ahead with the function. If so then just output something else as the third cell. Something along the lines of:
if((date1 == '') ||(date2 == ''))
{
nTable.rows[i].firstChild.nextSibling.nextSibling.lastChild.data = 'N/A';
return false;
}



Also is it possible to use another <table> above this date difference table?[/QUOTE]

Yes. To put a table before the date table then just change the [0] to [1] in the following line:

nTable = document.getElementsByTagName('TABLE')[0];

which means that it should be:

nTable = document.getElementsByTagName('TABLE')[1];

Here is the code with just the date table in the code:
Copy linkTweet thisAlerts:
@adalbyauthorJan 09.2006 — Thanks konithomimo, the table number worked great. When I insert the if statement code I get a java error. I have inserted this a few places within the existing script and still stops at the missed date row.
Copy linkTweet thisAlerts:
@konithomimoJan 09.2006 — Try this:
function calcDays(){

<i> </i>nTable = document.getElementsByTagName('TABLE')[1];
<i> </i>nDateSets = nTable.rows.length-1;
var j=0;
var cells = document.getElementsByTagName('td');
for (i=0; i&lt;=nDateSets; i++)
{
var date1 = nTable.rows[i].firstChild.lastChild.data;
var date2 = nTable.rows[i].firstChild.nextSibling.lastChild.data;
if((cells[j].innerText == '') || (cells[j+1].innerText = ''))
{
cells[j+2].innerText = 'N/A';
j=j+3;
}
else{ date1 = date1.split("-");
date2 = date2.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
nTable.rows[i].firstChild.nextSibling.nextSibling.lastChild.data = daysApart;
j=j+3;
}}
}
Copy linkTweet thisAlerts:
@adalbyauthorJan 09.2006 — I don't get any output at all now just a java error.
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — Just put this script in your header, and then call populate() however you want:
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{4}-\d{2}-\d{2}");
if(d.match(against))
{
return true;
}
}

function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[1].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i;
for(i=0;i&lt;c.length;i+=3)
{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("-");
date2 = c[i+1].innerText.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
}

else
c[i+2].innerText="N/A";
}
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorJan 11.2006 — Still not working out fo me. I have stripped all html to just what I need below.

<html>

<head>

<title></title>

<script type="text/javascript">

function myvalid(d)

{

var against = new RegExp("d{4}-d{2}-d{2}");

if(d.match(against))

{

return true;

}

}

function populate()

{

var date1, date2;

var tab = document.getElementsByTagName('table')[1].id;

var t = document.getElementById(tab);

var r = t.getElementsByTagName('tr');

var c = t.getElementsByTagName('td');

var i;

for(i=0;i<c.length;i+=3)

{

if(myvalid(c[i].innerText) && myvalid(c[i+1].innerText))

{

date1 = c[i].innerText.split("-");

date2 = c[i+1].innerText.split("-");

var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);

var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);

var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));

c[i+2].innerText = daysApart;

}



else

c[i+2].innerText="N/A";

}

}







</script>



</head>



<body onload="populate();">



<table border=1>

<tr>

<td>2005-11-1</td>

<td>2005-11-4</td>

<td align='middle'>&nbsp</td>

</tr>



<tr>
<td>2005-11-1</td>
<td> </td>
<td align='middle'>&nbsp</td>
</tr>

<tr>
<td>2005-11-1</td>
<td>2005-11-4</td>
<td align='middle'>&nbsp</td>
</tr>


</table>

</body>

</html>
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — The reason that the above script wont work is because I have a validate function that checks to see if the date is in the format:

XXX-XX-XX

it can't be XXXx-X-X

I could change it to accept that though
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — Change this line:

var against = new RegExp("d{4}-d{2}-d{2}");

to this:

var against = new RegExp("d{4}-d{1,2}-d{1,2}");
Copy linkTweet thisAlerts:
@adalbyauthorJan 11.2006 — I would need it to be in this format 2005-12-15 (xxxx-xx-xx)
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — If you make the change that I gave in my last post then you can have any of the following four formats:

XXXX-XX-XX

XXXX-XX-X

XXXX-X-XX

XXXX-X-X
Copy linkTweet thisAlerts:
@adalbyauthorJan 11.2006 — just a java error, do I need to use a body onload with this?

onload="populate();"
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — you can call if however you want. It can be from an onlick, onload, onchange, any type of trigger method.
Copy linkTweet thisAlerts:
@adalbyauthorJan 11.2006 — This is exactly how I have it just like this and no go. I'm fine with the onload how it is. Does this work for you?

<html>

<head>

<title></title>

<script type="text/javascript">

function calcDays(date1,date2)

{

date1 = date1.split("-");

date2 = date2.split("-");

var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);

var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);

var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));

return daysApart;

}


function doIt()

{

var cells = document.getElementsByTagName('td');

var i;

var day1, day2;

for(i=0;i<cells.length;i++)

{

if((i%3) == 0)

{

day1 = cells[i].data;

day2 = cells[i+1].data;

cells[i+2].data = calcDays(day1,day2);

}

}

}



window.onload = doIt;

</script>



</head>



<body >



<table border=1>

<tr>

<td>2005-11-01</td>

<td>2005-11-04</td>

<td align='middle'> &nbsp</td>

</tr>



<tr>
<td>2005-11-01</td>
<td>2005-11-01</td>
<td align='middle'>&nbsp</td>
</tr>

<tr>
<td>2005-11-01</td>
<td>2005-11-04</td>
<td align='middle'>&nbsp</td>
</tr>


</table>

</body>

</html>
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — The reason your's wont work is because I have the script set to the second table (and your example only has one table) in the body, as well you need to give the table an ID (since I call the table via its ID).
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;

&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{4}-\d{1,2}-\d{1,2}");
if(d.match(against))
{
return true;
}
}

function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[1].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i;
for(i=0;i&lt;c.length;i+=3)
{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("-");
date2 = c[i+1].innerText.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
}

else
c[i+2].innerText="N/A";
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="populate()"&gt;
&lt;table&gt;&lt;/table&gt;
&lt;table border=1 id="table1"&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&amp;nbsp&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td align='middle'&gt;&amp;nbsp&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&amp;nbsp&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — Also, you can remove the &nbsp if you want and you can also do it without an ID, but the ID is the easiest way because then you don't have to change the script if you add in more tables before the table you want to affect.
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{4}-\d{1,2}-\d{1,2}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var t = document.getElementsByTagName('table')[1];
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i;
for(i=0;i&lt;c.length;i+=3)
{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("-");
date2 = c[i+1].innerText.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="populate()"&gt;
&lt;table&gt;&lt;/table&gt;
&lt;table border=1&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;


Of course, the way I have it you have to change the table # anyway, but you can alswys hard code in the ID of the table that you want:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{4}-\d{1,2}-\d{1,2}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var t = document.getElementById('table1');
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i;
for(i=0;i&lt;c.length;i+=3)
{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("-");
date2 = c[i+1].innerText.split("-");
var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);
var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="populate()"&gt;
&lt;table&gt;&lt;/table&gt;
&lt;table border=1 id="table1"&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2005-11-1&lt;/td&gt;
&lt;td&gt;2005-11-4&lt;/td&gt;
&lt;td align='middle'&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@adalbyauthorJan 11.2006 — Yes konithomimo, this is finally it! It works exactly like I need it to. Thank you for all your hard work. And also thanks to everyone who has helped on this post. I really appreciate it!
Copy linkTweet thisAlerts:
@konithomimoJan 11.2006 — No problem. Good luck with the rest of your coding.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 03.2006 — Hi all,

I have been asked to change the date format to 12/21/2005 instead of the below format of 2005-12-21 to calculate day difference. I have tried to change to format of the var sDate and var eDate but that didn't work. Any ideas on how to just switch the format?

Thanks,

Aaron


<script type="text/javascript">

function myvalid(d)

{

var against = new RegExp("d{4}-d{1,2}-d{1,2}");

if(d.match(against))

{

return true;

}

}

function populate()

{

var date1, date2;

var t = document.getElementById('table1');

var r = t.getElementsByTagName('tr');

var c = t.getElementsByTagName('td');

var i;

for(i=0;i<c.length;i+=3)

{

if(myvalid(c[i].innerText) && myvalid(c[i+1].innerText))

{

date1 = c[i].innerText.split("-");

date2 = c[i+1].innerText.split("-");

var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);

var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);

var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));

c[i+2].innerText = daysApart;

}

}

}

</script>
Copy linkTweet thisAlerts:
@konithomimoFeb 03.2006 — Change this:

var against = new RegExp("d{4}-d{1,2}-d{1,2}");

to this:

var against = new RegExp("d{1,2}-d{1,2}-d{4}");

That will allow the slash and the hyphen. To not allow the hyphens simply remove them from the above line.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 03.2006 — Just getting a n/a for the output. Should I rearrange these lines somehow?

var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);

var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);
Copy linkTweet thisAlerts:
@konithomimoFeb 03.2006 — Just getting a n/a for the output. Should I rearrange these lines somehow?

var sDate = new Date(date1[1]+"/"+date1[2]+"/"+date1[0]);

var eDate = new Date(date2[1]+"/"+date2[2]+"/"+date2[0]);[/QUOTE]


yes. It should be:

&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var t = document.getElementById('table1');
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i;
for(i=0;i&lt;c.length;i+=3)
{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
}
}
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 03.2006 — Yes thats It, Thanks!

One more question, not sure if it even possible without re-engineering everything which I do NOT want to do.

Is it possible to add a <td> before the first date that I can populate with any text? Right now it stops calculating when I add in an extra <td>. Currently My html code looks like this:

<tr valign="middle">
<td width="13%" align="center" class="offf">12/15/2005</td>
<td width="22%" align="center" class="offf">12/19/2005</td>
<td width="22%" align="center" class="offf"></td>
</tr>



I would like it to look like this:

<tr valign="middle">
<td width="22%" align="center" class="offf">Text!!!!!!</td>
<td width="13%" align="center" class="offf">12/15/2005</td>
<td width="22%" align="center" class="offf">12/19/2005</td>
<td width="22%" align="center" class="offf"></td>
</tr>
Copy linkTweet thisAlerts:
@konithomimoFeb 03.2006 — Just change i=0 to i=1 in the following line:

for(i=0;i<c.length;i+=3)


it should be:

for(i=1;i<c.length;i+=3)

That way it won't check the first cell of the table.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 03.2006 — Yes that worked konithomimo! Your on a roll! One more for ya if you ever get time to tackle something else. Is it possible to Average all day differences at the bottom without re-coding everything? Just curious...

Here is the final code I'm using




<script type="text/javascript">

function myvalid(d)

{

var against = new RegExp("d{1,2}[/]d{1,2}[/]d{4}");

if(d.match(against))

{

return true;

}

}

function populate()

{

var date1, date2;

var tab = document.getElementsByTagName('table')[5].id;

var t = document.getElementById(tab);

var r = t.getElementsByTagName('tr');

var c = t.getElementsByTagName('td');

var i;

for(i=1;i<c.length;i+=3)

{

if(myvalid(c[i].innerText) && myvalid(c[i+1].innerText))

{

date1 = c[i].innerText.split("/");

date2 = c[i+1].innerText.split("/");

var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);

var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);

var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));

c[i+2].innerText = daysApart;

}



else

c[i+2].innerText="n/a";

}

}





window.onload=populate;

</script>









Thanks,

Aaron
Copy linkTweet thisAlerts:
@konithomimoFeb 04.2006 — Add one more cell to the very bottom. Then use this code:
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[5].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i, tot=0;
for(i=1;i&lt;c.length;i+=3)

{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
tot+=daysApart;
if(i==(c.length-2))
c[i+1].innerText = tot;
}

else
c[i+2].innerText="n/a";
}
}

window.onload=populate;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@HDCFeb 04.2006 — When I add this to the very bottom I get a java error and all I get in the second <td> is n/a

<tr valign="middle">
<td align="center">&nbsp; </td>
<td align="center">&nbsp; </td>
<td align="center">&nbsp; </td>
<td align="center">&nbsp; </td>
</tr>





Sorry, I have been following this post as well.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 04.2006 — thanks konithomimo, yes, I'm also getting the same thing as HDC with an error and just an n/a. Should my bottom cells be within the same table? I do think they should , correct...
Copy linkTweet thisAlerts:
@konithomimoFeb 04.2006 — Sorry. These two lines:

if(i==(c.length-2))

c[i+1].innerText = tot;

should be:

if(i==(c.length-4))

c[i+3].innerText = tot;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 04.2006 — That cleared up my javascript error and now I just have an n/a at the bottom in the 4th column(same column as my day difference numbers) where the average is suppose to be. Any idea's?
Copy linkTweet thisAlerts:
@konithomimoFeb 04.2006 — I would have to see the page to know what is messing up.
Copy linkTweet thisAlerts:
@konithomimoFeb 04.2006 — I just reread one of your posts and saw that you added in 4 more cells, which means that you would need to make it:

if(i==(c.length-7))

c[i+6].innerText = tot;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 04.2006 — I have 4 columns total they look like this:

<tr>

<td>Name</td>

<td>02/01/2006</td>

<td>02/03/2006</td>

<td> "Day Difference goes here" </td>

</tr>

When I add to the very bottom a new <tr> set(below) for the average it looks like the script is acting like its trying to do the date diff and then just outputs the n/a. Should I have an id=average or something like that in my last <td>?

<tr>

<td></td>

<td></td>

<td></td>

<td></td>

</tr>

I have replaced the "Name" with a row number but here is the link to my live example which may help:

http://www.heartlanddentalcare.com/wd/
Copy linkTweet thisAlerts:
@konithomimoFeb 05.2006 — The if statement should have been outside of the first if statement of the for loop. Your script should be:
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[5].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i, tot=0;
for(i=1;i&lt;c.length;i+=4)

{
if(i==(c.length-7))
{
c[i+6].innerText = tot;
return true;
}

if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
tot+=daysApart;
}

else
c[i+2].innerText="n/a";
}
}

window.onload=populate;
&lt;/script&gt;


You can make it the average if you want.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — thanks konithomimo that did the trick for summing all day diffs at the bottom, how could I average them?
Copy linkTweet thisAlerts:
@konithomimoFeb 05.2006 — Just create a new variable that starts at 0 and then add one to it every time that both cells are dates:

&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[5].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i, tot=0, avg=0;
for(i=1;i&lt;c.length;i+=4)

{
if(i==(c.length-7))
{
c[i+6].innerText = (tot/avg);
return true;
}

if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
tot+=daysApart;
avg++;
}

else
c[i+2].innerText="n/a";
}
}

window.onload=populate;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — That does it great! I'm trying to round the average to 1 decimal place and am not too sure where to include this to do it. Any idea on this?

.toFixed(1);
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — I think I've got it.

c[i+6].innerText = (tot/avg).toFixed(1);

Seemed to work ok so far.

Thanks for all your help konithomimo, I owe you a few!

Aaron
Copy linkTweet thisAlerts:
@konithomimoFeb 05.2006 — Sounds good. Also, I have been meaning to tell you that you can remove this line:

var r = t.getElementsByTagName('tr');

it isnt needed.
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — konithomimo, I have noticed that the very last date diff row is skipped and leaves out the difference. Right under that is the average. In clue to why? Other than that it's calculating everything great.
Copy linkTweet thisAlerts:
@konithomimoFeb 05.2006 — yeah. i accidently put the if statement in the wrong place:
&lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[5].id;
var t = document.getElementById(tab);
var c = t.getElementsByTagName('td');
var i, tot=0, avg=0;
for(i=1;i&lt;c.length;i+=4)

{
if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
tot+=daysApart;
avg++;
}

if(i==(c.length-7))
{
c[i+6].innerText = (tot/avg).toFixed(1);
return true;
}

else
c[i+2].innerText="n/a";
}
}

window.onload=populate;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — Sorry, now I get a n/a for all the day diffs except the last cell which calculates it right. Haha no problem man your doing an excellent job and have supurb patients!!!!
Copy linkTweet thisAlerts:
@konithomimoFeb 05.2006 — &lt;script type="text/javascript"&gt;
function myvalid(d)
{
var against = new RegExp("\d{1,2}[/]\d{1,2}[/]\d{4}");
if(d.match(against))
{
return true;
}
}
function populate()
{
var date1, date2;
var tab = document.getElementsByTagName('table')[5].id;
var t = document.getElementById(tab);
var r = t.getElementsByTagName('tr');
var c = t.getElementsByTagName('td');
var i, tot=0, avg=0;
for(i=1;i&lt;c.length;i+=4)

{
if(i==(c.length-7))
{
c[i+6].innerText = (tot/avg);
}

if(myvalid(c[i].innerText) &amp;&amp; myvalid(c[i+1].innerText))
{
date1 = c[i].innerText.split("/");
date2 = c[i+1].innerText.split("/");
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
c[i+2].innerText = daysApart;
tot+=daysApart;
avg++;
if(i==(c.length-7))
return true;
}

else
c[i+2].innerText="n/a";
}
}

window.onload=populate;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@adalbyauthorFeb 05.2006 — konithomimo, thats it works perfectly. Thanks for all your help!

Aaron
×

Success!

Help @adalby spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...