Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > counting up instead of counting down

Reply
Thread Tools

counting up instead of counting down

 
 
edwardfredriks
Guest
Posts: n/a
 
      09-05-2005
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

 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      09-06-2005
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"==1?"day ago"== -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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      09-06-2005
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
 
Reply With Quote
 
edwardfredriks
Guest
Posts: n/a
 
      09-06-2005
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"==1?"day ago"== -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


 
Reply With Quote
 
edwardfredriks
Guest
Posts: n/a
 
      09-06-2005
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

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      09-06-2005
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"==1?"day ago"== -1?"day from now":"days from now";
document.write(pastDate+" "+(x>0? "was":"is")+" "+x+" "+days)
</script>

Mick
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      09-07-2005
JRS: In article <df5Te.81249$(E-Mail Removed)>, dated
Tue, 6 Sep 2005 00:18:17, seen in news:comp.lang.javascript, Mick White
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting down an integer vector from the highest degree (111) to the lowest degree (000) Matt Chwastek C++ 6 11-20-2006 10:51 PM
counting down or up is faster sololoquist C Programming 5 11-20-2006 07:00 PM
Counting down faster when looping? Ulf Nordlund Java 26 02-09-2005 02:00 PM
Counting down to cookie expiration Bash Javascript 0 02-09-2005 08:02 AM
COMBO BOX INSTEAD OF DROP DOWN BOX =?Utf-8?B?QW5nZWw=?= ASP .Net 3 06-29-2004 06:38 PM



Advertisments