PDA

View Full Version : Count down - incorrect by 1 day


miamaria
03-12-2003, 12:31 PM
I've used a script from Javascript Kit "Count down to my birthday"
and the first part looks like this:
var now = new Date();
var birthd = new Date("March 31, 2003");
var toGo = birthd.getTime() - now.getTime();
var days = Math.floor(toGo / (1000 * 60 * 60 * 24));

Problem is, when calculating the days left to the 31st of March 2003, it comes up with one day less than correct. Example:
Todays the 12th of March - and the counter says it's 18 days left to (my sons) birthday, when in fact it's 19 . How do I change it without changing the "var birthd to April 01, 2003"?

The full script I used is on http://wsabstract.com/script/script2/countbirth.shtml

Thanks a bunch!:thumbsup:

justame
03-12-2003, 12:46 PM
mia...
/me just a thinks® the script counts down tilll the just a special® day...aka theres just a 18® days to pass n' on the 19th isss his day...

n' sorrry.../me doesnt just a know® howww to change that script unless sommmewhere??? in this part???
var toGo = birthd.getTime() - now.getTime();

you can just a tell® it to +1 ??? hehehe...dont worry.../me is just a sure® one of the pros will know howww to fix it...:O)))

just a ps®...
/me just a sees® thattt twas your first post...sooo...just a welcome® to the forum...:O)))

whammy
03-13-2003, 12:08 AM
The key part is this:

Math.floor

var days = Math.floor(toGo / (1000 * 60 * 60 * 24));

Since it's over 18, but NOT QUITE 19 days until his birthday, that's why you get 18. Math.floor rounds down.

You could use Math.ceil instead. :)

whammy
03-13-2003, 12:11 AM
P.S. Math.ceil rounds up.

By the way, Hi Stephanie! Long time no talk. :)

I wonder what it's like for someone to be introduced to "justamese" in their first post. ;) justame, what happened to your translator scripts? If you give me the "rules" of justamese I'd like to write another, lol. I wasn't as good when you first met me but I do this kinda stuff all the time now. :D

And welcome to the forum, miamaria! :D

Skyzyx
03-13-2003, 06:59 AM
Also, if you happen to be dealing with years in this script, remember that there are NOT 365 days in a year, but 365.25 days in a year.

This particular oversight messed me up at one point...

miamaria
03-16-2003, 02:04 PM
Cheers for all the help and the welcome msg's!

I'm off to make it work! See you soon! *poof*