By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

counting up instead of counting down

P: n/a
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

Sep 6 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
edwardfredriks wrote:
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
Sep 6 '05 #2

P: n/a
edwardfredriks wrote:
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>
--
Rob
Sep 6 '05 #3

P: n/a
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

Mick White wrote:
edwardfredriks wrote:
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


Sep 6 '05 #4

P: n/a
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

Sep 6 '05 #5

P: n/a
edwardfredriks wrote:
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
Sep 6 '05 #6

P: n/a
JRS: In article <df*******************@twister.nyroc.rr.com>, dated
Tue, 6 Sep 2005 00:18:17, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :
edwardfredriks wrote:
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
...

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 .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 7 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.