简体   繁体   English

JS cookie 每周六过期

[英]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.我需要能够使用 Javascript 设置一个 cookie 并让它在每个星期六到期,但不确定从哪里开始。

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

document.cookie =文档.cookie =

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.如果你console.log(nextSaturday)它会给你下周六的日期。 Then you can store your cookie like this:然后你可以像这样存储你的cookie:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM