javascriptcookiessetcookieexpired-cookies

Expire JS cookie every Saturday


I need to be able to set a cookie and have it expire every saturday using Javascript but not sure where to begin.

var now = new Date(); var day = now.getDay();

document.cookie =


Solution

  • First you have to define next Saturday this way.

    var nextSaturday = new Date();
    nextSaturday.setDate(nextSaturday.getDate() + (6 + 7 - nextSaturday.getDay()) % 7);
    

    if you console.log(nextSaturday) it will give you the date of next Saturday. Then you can store your cookie like this:

    document.cookie = "myCookie=ok; expires=" + nextSaturday;