countdown script problem (months remaining occasionally incorrect)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph S

    countdown script problem (months remaining occasionally incorrect)

    I am having problems with the following countdown script. For some reason,
    the number of months is occasionally wrong. I can't quite put my finger on
    the problem. Any assistance or recommendation from someone more knowlegable
    than me would be appreciated. Thanks much.

    <script>
    ty=2005;to=08;t d=31;th=24;tm=0 ;ts=0;
    function e(){
    ny=0;no=0;nd=0; nh=0;nm=0;ns=0; n=new Date();

    if(n.getYear()> ty){t.s.value=0 ;t.m.value=0;t. h.value=0;t.d.v alue=0;t.o.valu e
    =0;t.y.value=0; }
    else{
    ns=ns+ts-n.getSeconds(); if(ns<0){ns=60+ ns;nm=-1;}t.s.value=ns ;
    nm=nm+tm-n.getMinutes(); if(nm<0){nm=60+ nm;nh=-1;}t.m.value=nm ;
    nh=nh+th-n.getHours();if (nh<0){nh=24+nh ;nd=-1;}t.h.value=nh ;
    nd=nd+td-n.getDate();if( nd<0){
    x=n.getMonth();
    if(x==0||x==2|| x==4||x==6||x== 7||x==9||x==11) {nd=31+nd;}
    if(x==3||x==5|| x==8||x==10){nd =30+nd;}

    if(x==1){if(n.g etYear()/4-Math.floor(n.ge tYear()/4)==0){nd=29+nd ;}else{nd=28
    +nd;}}
    }no=-1;t.d.value=nd;
    no=no+to-n.getMonth();if (no<0){no=11+no ;ny=-1;}t.o.value=no ;

    ny=ny+ty-n.getYear();if( ny<0){t.y.value =0;}else{t.y.va lue=ny;setTimeo ut("e()
    ",1000);}
    }
    }
    </script>

    Example:



    ---------------
    Joseph
    icynospamstorm@ hotnospammail.c om

    (Please remove both occurrences of "nospam" to reply via email, or simply
    reply here in USENET instead.)



  • Lasse Reichstein Nielsen

    #2
    Re: countdown script problem (months remaining occasionallyinc orrect)

    "Joseph S" <nospam@nospam. org> writes:
    [color=blue]
    > I am having problems with the following countdown script. For some reason,
    > the number of months is occasionally wrong. I can't quite put my finger on
    > the problem. Any assistance or recommendation from someone more knowlegable
    > than me would be appreciated. Thanks much.[/color]

    Measuring difference in time in months is not well defined.
    How many months and days are there between
    2004-02-29 and 2004-05-31 (92 days)
    or between
    2004-03-01 and 2004-06-01 (also 92 days)
    or even
    2004-01-29 and 2004-02-29 (29 days)
    and
    2004-01-30 and 2004-03-01 (also 29 days)

    If you can answer this in a way that you are satisfied with ... does your
    program agree with you?
    [color=blue]
    > <script>[/color]

    It's <script type="text/javascript">. The type attribute is required
    in HTML 4.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: countdown script problem (months remaining occasionally incorrect)

      JRS: In article <e2_Pb.12721$zy 3.2450@okepread 01>, seen in
      news:comp.lang. javascript, Joseph S <nospam@nospam. org> posted at Thu,
      22 Jan 2004 18:37:49 :-
      [color=blue]
      >I am having problems with the following countdown script. For some reason,
      >the number of months is occasionally wrong. I can't quite put my finger on
      >the problem.[/color]

      The code does what you have programmed it to do, but it is badly written
      and your intent is concealed.

      Obviously, though, you are not making effective use of the properties of
      a date object. See sig at foot.

      You have n.getYear()/4-Math.floor(n.ge tYear()/4)==0
      which should probably be n.getYear()%4 == 0 except that you have no
      need for that part of the code at all.


      If this is course-work requiting you to use inefficient techniques in
      order to exercise your programming skill, then you should say so.

      If you have found the approach recommended in a book, except as above,
      burn the book.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Joseph S

        #4
        Re: countdown script problem (months remaining occasionally incorrect)

        "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote:[color=blue]
        > news:comp.lang. javascript, Joseph S <nospam@nospam. org> posted at Thu,
        > 22 Jan 2004 18:37:49 :-
        >[color=green]
        > >I am having problems with the following countdown script. For some[/color][/color]
        reason,[color=blue][color=green]
        > >the number of months is occasionally wrong. I can't quite put my finger[/color][/color]
        on[color=blue][color=green]
        > >the problem.[/color]
        >
        > The code does what you have programmed it to do, but it is badly written
        > and your intent is concealed.
        >
        > Obviously, though, you are not making effective use of the properties of
        > a date object. See sig at foot.
        >
        > You have n.getYear()/4-Math.floor(n.ge tYear()/4)==0
        > which should probably be n.getYear()%4 == 0 except that you have no
        > need for that part of the code at all.
        >
        >
        > If this is course-work requiting you to use inefficient techniques in
        > order to exercise your programming skill, then you should say so.[/color]

        Nah. I'm just wasn't sure what I was doing. That's why I posted.
        [color=blue]
        > If you have found the approach recommended in a book, except as above,
        > burn the book.[/color]

        Thanks for the free advice.

        Joseph



        Comment

        Working...