countdown script problem (months remaining occasionally incorrect)

J

Joseph S

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;td=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.value=0;t.o.value
=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.getYear()/4-Math.floor(n.getYear()/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.value=ny;setTimeout("e()
",1000);}
}
}
</script>

Example:

http://members.cox.net/icystorm/retire

---------------
Joseph
(e-mail address removed)

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

Lasse Reichstein Nielsen

Joseph S said:
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.

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?

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

/L
 
D

Dr John Stockton

JRS: In article <e2_Pb.12721$zy3.2450@okepread01>, seen in
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.

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.getYear()/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.
 
J

Joseph S

Dr John Stockton said:
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.getYear()/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.

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

Thanks for the free advice.

Joseph
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top