Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating and displaying result of # days between dates

Status
Not open for further replies.

pantichd

Programmer
Nov 12, 2002
73
US
Hello,

I want to display a message like this:
Day x of construction.

To do this I want to have a function in a js file that takes two dates, calculates their difference and returns an integer value.

I have a file called functions.js which has a bunch of functions I'm using in my site. One of those functions is:
function daysDiff(begDate, endDate) {
alert("testmsg")
var diffMillSec = endDate.getTime() - begDate.getTime()
return Math.ceil(diffMillSec / oneDay()
}

My html looks like this:

<p>Day <script language="JavaScript">daysDiff(new Date(2005,9,31), new Date())</script> of construction</p>

When I run it nothing displays where I expect the numeric value.

What am I doing wrong?

Thanks in advance for any help.

David.
 
If you copied and pasted your code above from the web page, you have an extra opening parenthesis on the line:

return Math.ceil(diffMillSec / oneDay[red]([/red]);

Lee
 
DUH! My bad. I can't believe I missed that. Wish I had an editor that did syntax checking for Java Script.

Anyway, even after fixing that stupid error : ) it doesn't work.

Do I need to do something spsecial to write the result of the function?

Thanks!
 
>[tt] Math.ceil(diffMillSec / oneDay()[/tt]
I think Lee meant
[tt] Math.ceil(diffMillSec / oneDay()[red])[/red][/tt]

Also what is oneDay()? Whatever function it is, it should return 24*60*60*1000, as simple as that - why a function for the sake of a function?

 
oneDay() does exactly that.

It's not just a function for the sake of it. I use that value in a lot of different places. Instead of hardcoding it in each place I just have a function that returns that value.

So how do I write out the result of calling my daysDiff() function?
 
[tt]function oneDay() {
return 24*60*60*1000;
}
[/tt]bare minimum.
 
>So how do I write out the result of calling my daysDiff() function?
With the script put like you put, it is something very terrible.
[tt]
document.write(Math.ceil(diffMillSec / oneDay()))
[/tt]
It is fundamentally no good. Make out a span at that place and write it out there.
[tt]<p>Day&nbsp;<span id="x"></span>&nbsp;of construction</p>
[/tt]
and put the function in the window.onload handler. Within the function, it does not "return" but write to it.
[tt]
document.getElementById("x").innerHTML=Math.ceil(diffMillSec / oneDay());
[/tt]
 
>>When I run it nothing displays where I expect the numeric value

If you get nothing and even the alert box w/ "testmsg", i wonder whether daysDiff() is executed or not.

Have you import function.js to HTML file? Something like that
Code:
<SCRIPT LANGUAGE="JavaScript" SRC="function.js">
</SCRIPT>
 
Run your script in Firefox and see what the errors the Javascript console shows. They'll probably be fairly meaningful, and will avoid guesswork when we haven't got a complete set of code available to us to really know what is going on.

Are you sure you're including the oneDay function on the page? Does the "testmsg" alert ever show? If not, I suspect your error lies outside of that function (assuming you've fixed the syntax error pointed out earlier).

One thing to try - add semicolons to the ends of your statements. At the moment, you've nothing:

Code:
function oneDay() {
   return (24 * 60 * 60 * 1000);
}

function daysDiff(begDate, endDate) {
   alert('testmsg')[b];[/b]
   var diffMillSec = endDate.getTime() - begDate.getTime()[b];[/b]
   return (Math.ceil(diffMillSec / oneDay()));
}

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks everyone for your replies.

The problem was that I didn't know about the "document.write" method.

After fixing the original problem (missing a parentheses) it was showing the "testmsg" popup but still no output.

Once adding the document.write call I have output. So now the final snippet looks like this:

<p>Day
<script language="JavaScript">
document.write(daysDiff(new Date(2005,9,31), new Date())) </script>
of construction</p>

Thanks again for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top