[thelist] JS calculations on Date object

Steve Webster steve at netbreed.co.uk
Mon Sep 30 15:08:00 CDT 2002

fstorr wrote:

>Cheers for that Steve and also for the explanation - very helpful.  One
>question - if I set todaysDate and expireDate to the same day, daysLeft
>is equal to -1.  Shouldn't this be 0 (and if so, I'll whack a +1 at the
>end of the line)?
Surely that's not necessary, since if today's date is the expiry date
then you'll be redirecting the visitor?

If you really need to fix this, then add the following like of code
after creating the Date object for todaysDate...

    todaysDate.setTime(todaysDate.getTime() - (todaysDate.getTime() %
(1000 * 60 * 60 * 24)) + (todaysDate.getTimezoneOffset() * 60 * 1000));

This removes the time element of the current date, factoring in the
timezone difference, so that both dates fall on midnight of their
respective days, making calculations much easier!



More information about the thelist mailing list