counting up instead of counting down

Discussion in 'Javascript' started by edwardfredriks, Sep 6, 2005.

  1. 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
     
    edwardfredriks, Sep 6, 2005
    #1
    1. Advertising

  2. edwardfredriks

    Mick White Guest

    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
     
    Mick White, Sep 6, 2005
    #2
    1. Advertising

  3. edwardfredriks

    RobG Guest

    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
     
    RobG, Sep 6, 2005
    #3
  4. 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
     
    edwardfredriks, Sep 6, 2005
    #4
  5. 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
     
    edwardfredriks, Sep 6, 2005
    #5
  6. edwardfredriks

    Mick White Guest

    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
     
    Mick White, Sep 6, 2005
    #6
  7. JRS: In article <df5Te.81249$>, dated
    Tue, 6 Sep 2005 00:18:17, seen in news:comp.lang.javascript, Mick White
    <> 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.
     
    Dr John Stockton, Sep 7, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    639
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. Ulf Nordlund

    Counting down faster when looping?

    Ulf Nordlund, Feb 7, 2005, in forum: Java
    Replies:
    26
    Views:
    868
    Walter Mitty
    Feb 9, 2005
  3. sololoquist

    counting down or up is faster

    sololoquist, Nov 20, 2006, in forum: C Programming
    Replies:
    5
    Views:
    344
    Tor Rustad
    Nov 20, 2006
  4. Matt Chwastek
    Replies:
    6
    Views:
    542
    Michael Angelo Ravera
    Nov 20, 2006
  5. Bash

    Counting down to cookie expiration

    Bash, Feb 9, 2005, in forum: Javascript
    Replies:
    0
    Views:
    92
Loading...

Share This Page