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?
Who is Participating?
Ivo StoykovConnect With a Mentor Commented:
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.

All Courses

From novice to tech pro — start learning today.