PDA

View Full Version : Cookies and Dates...Not the edible kind.


Tanker
10-11-2002, 06:01 PM
I want to set a cookie that will expire at midnight the following day...

Say I goto the site, It sets a cookie to last until the next day at midnight, The cookie is set at 1140am on Tue the 4th, it should expire at 1159pm on Wed the 5th.

I think im close but i've never been good with dates. Any help would be appreciated.


function cookie_24(cookieName,cookieValue){
var now = new Date();
now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);
expire=new Date(now).toGMTString()
document.cookie = cookieName + "=" + cookieValue + "; expires=" + expire + "; path=/;"
}

Mr J
10-11-2002, 07:05 PM
See if this works

The lines added gets the current time, deducts that from 23:59 to give you the time remaining for the current day.
The remaining time is then added to the cookie time




function cookie_24(cookieName,cookieValue){
var now = new Date();

nowhr=now.getHours()
nowmin=now.getMinutes()
diffhr=23-nowhr
diffmin=59-nowmin

now.setTime(now.getTime() + 1 * (24+diffhr) * (60+diffmin) * 60 * 1000);
expire=new Date(now).toGMTString()
document.cookie = cookieName + "=" + cookieValue + "; expires=" + expire + "; path=/;"
}