• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Javascript cookies clear the next day

Hi, we're trying to set a cookie using javascript that doesn't expiry for at least 6 month.

We're using this function to set a cookie

function setCookie(name, value, seconds) {
      if (typeof(seconds) != 'undefined') {
            var date = new Date();
            date.setTime(date.getTime() + (seconds*1000));
            var expires = "; expires=" + date.toGMTString();
            } else {
            var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";

and then this to initiate the cookie...

The cookie seems to work ok until the next day when you log into the site and the cookie has been cleared.

Am I doing something wrong?
1 Solution
Ivo StoykovCommented:
try this


Ivo Stoykov
var date = new Date();
date.setTime(date.getTime() + (seconds*1000));
date.setMonth(date.getMonth()+6); // <-- add 6 months to current date 

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now