counting up instead of counting down

E

edwardfredriks

I'm looking for a script that, instead of counting down, can "count up"
from a given date. So the output should be something like "(xx) days
since (date/event)" or "(date/event) was (xx) days ago".

Does anybody know where to find a script like that, or could someone
code one for me?

Thanks in advance.

Yours,
Edward Fredriks
 
M

Mick White

edwardfredriks said:
I'm looking for a script that, instead of counting down, can "count up"
from a given date. So the output should be something like "(xx) days
since (date/event)" or "(date/event) was (xx) days ago".

Does anybody know where to find a script like that, or could someone
code one for me?
Something like:

<script type="text/javascript">
function daysSince(dateObj){
return Math.floor((new Date()-dateObj)/864e5))
}

x=daysSince(new Date(2005,8,1))// Sep 1, 2005
var days=
x>1?"days ago":x==1?"day ago":x== -1?"day from now":"days from now";

</script>

Not entirely accurate, because of summer time and dependence on user's
clock.(And x could equal zero)

Mick
 
R

RobG

edwardfredriks said:
I'm looking for a script that, instead of counting down, can "count up"
from a given date. So the output should be something like "(xx) days
since (date/event)" or "(date/event) was (xx) days ago".

Does anybody know where to find a script like that, or could someone
code one for me?

Depending on your requirements, Mick's solution may suffice. But if
more rigour is required, check out:

<URL:http://www.merlyn.demon.co.uk/js-date1.htm#diff>
 
E

edwardfredriks

Tried it (that is copy/pasted it into a html-file) but it didn't write
anything write anything in the browser window. Is there something else
I should do? I have virtually no understanding of the workings of
JavaScript whatsoever, so could you try to keep it plain. Thank you.

Edward
 
E

edwardfredriks

I have virtually no understanding of JavaScript whatsoever so this
looks like all gibberish to me, I have no clue what to do with it,
other than to plainly copy/paste it to a html-file (which didn't do
much good). So please, could you explain what to do with it to make it
work? Thank you.

Edward
 
M

Mick White

edwardfredriks said:
Tried it (that is copy/pasted it into a html-file) but it didn't write
anything write anything in the browser window. Is there something else
I should do? I have virtually no understanding of the workings of
JavaScript whatsoever, so could you try to keep it plain. Thank you.

<script type="text/javascript">
function daysSince(dateObj){
return Math.floor((new Date()-dateObj)/864e5)
}
pastDate=new Date(2005,8,1)// Sep 1, 2005
x=daysSince(pastDate)
var days=
x>1?"days ago":x==1?"day ago":x== -1?"day from now":"days from now";
document.write(pastDate+" "+(x>0? "was":"is")+" "+x+" "+days)
</script>

Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 6 Sep 2005 00:18:17, seen in Mick White
Something like:

<script type="text/javascript">
function daysSince(dateObj){
return Math.floor((new Date()-dateObj)/864e5))
}

x=daysSince(new Date(2005,8,1))// Sep 1, 2005
...
Not entirely accurate, because of summer time and dependence on user's
clock.

If you use new Date(2005, 8, 1, 12) // noon 2005 Sep 01
and Math.round instead of Math.floor, all will be well except sometimes
for an hour at the start/end of a day; and that should be fixable by
appending .setHours(12) to new Date() .

For two dates given as Y1 M1 D1 Y2 M2 D2, however, it would be better to
use Date.UTC().

I wonder whether the OP thought to read the newsgroup FAQ, which should
have lead him to function DateDiff .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top