Expire JS cookie every Saturday

71 Views Asked by At

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 =

1

There are 1 best solutions below

1
On BEST ANSWER

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;