如何让这个cookie在14天后过期

Qui*_*r27 5 javascript cookies

function setcookie(cookieName, cookieValue, nDays) {
  var today = new Date();
  var expire = new Date();
  if (nDays == null || nDays == 0) nDays = 1;
  expire.setTime(today.getTime() + 3600000 * 24 * nDays); // changed that to * 14
  document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

function readcookie(cookieName) {
  var theCookie = " " + document.cookie;
  var ind = theCookie.indexOf(" " + cookieName + "=");
  if (ind == -1) ind = theCookie.indexOf(";" + cookieName + "=");
  if (ind == -1 || cookieName == "") return "";
  var ind1 = theCookie.indexOf(";", ind + 1);
  if (ind1 == -1) ind1 = theCookie.length;
  return unescape(theCookie.substring(ind + cookieName.length + 2, ind1));

}
Run Code Online (Sandbox Code Playgroud)

我试图改变nday到nday=14,但一无所获.

然后我尝试了expire.setTime(today.getTime() + 3600000 * 24 * 14),仍然没有

我只需要使用此代码让cookie在一定天数后过期.对不起,我刚刚开始本周刚开始的javascript.

Luc*_*ier 8

如果您始终要将Cookie设置为14天,请删除nDays参数并直接在expire.setTime方法中设置14天

function setcookie(cookieName,cookieValue) {
    var today = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 3600000*24*14);
    document.cookie = cookieName+"="+encodeURI(cookieValue) + ";expires="+expire.toGMTString();
}
Run Code Online (Sandbox Code Playgroud)