Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Cookie expiry time

7:48 pm on May 25, 2012 (gmt 0)

Junior Member

10+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0

I'm struggling a bit here. I found this code and I can't figure out how to be in total control of how long the cookie is stored for on the users machine. I've set it up but it seems to expire around 5 years time and I've no real idea why?!

I have this as as the cookie script:


function SetCookie(c_name,value,expiredays)
var exdate=new Date()
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())


and then when the user clicks this button it stores the cookie.

<a href="#" onClick="SetCookie('mycookie','mycookie','30')">Allow Cookies</a>

I just need to know how to set the expiry date for 365 days time?

Any ideas, help or advice would be much appreciated.
11:48 pm on May 28, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 5015
votes: 24

toGMTString is deprecated and should not be used. Use toUTCString instead.
Instead of setDate, use setTime:

exdate.setTime(exdate.getTime() + (1000 * 60 * 60 * 24 * expiredays));

setTime takes a value in ms, and getTime returns a value in ms. So get the current time in ms, then multiply your expiredays value by 24 to get the number of hours, multiply that by 60 to get the number of minutes, multiply that by 60 to get the number of seconds, and multiply that by 1000 to get the number of ms. Then, as I mentioned, use toUTCString instead of toGMTString.

Hope that helps.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members