Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get this cookie to expire after 14 days

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));

}

I tried changing nday to nday=14 but nothing.

Then I tried expire.setTime(today.getTime() + 3600000 * 24 * 14) , still nothing

I just need to use this code to get the cookie to expire after a set number of days. Sorry i am new to javascript just started this week.

like image 332
Quinnystar27 Avatar asked Sep 10 '15 09:09

Quinnystar27


People also ask

How do I extend a cookie expiry date?

Extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the 'expires' attribute to a date and time.

How do you set cookies to expire in 30 days?

time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

How do I set never expiring cookie?

Syntax: document. cookie = "cookieName= true; expires=Tue, 19 Jan 2038 04:14:07 GMT"; // OR const cookieName = "something"; const cookieValue = "something"; const daysToExpire = new Date(2147483647 * 1000).

How long do cookies last?

How do cookies expire? Cookies expire when they are no longer fresh and can be considered expired. They will not be accepted by the server if they have been expired for over 90 days. How long do browser cookies last? Cookies last for a certain amount of time.

How to set up cookies that never expire?

Disclaimer: All the cookies expire as per the cookie specification. So, there is no block of code you can write in JavaScript to set up a cookie that never expires. It is just impossible and is a fact.

How to check cookies expiry date in Firefox?

After installing the Cookies Manager+ add-on, you can access it via the Firefox menu -> Web developer -> Cookies Manager+. Selecting the cookie you want to examine will also list its expiry date at the bottom of the window, and if you want to edit it, simply click the button.

How do I change the date on my Cookie?

Right click on the Key and select Edit The Cookie’s Content which will open a new window where you can change the date either by typing in directly or from the pop up calender. Then press Modify Cookie and confirm.


2 Answers

If you always want to set your cookies for 14 days, delete your nDays parameter and set 14 days directly in the expire.setTime method

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();
}
like image 95
Luc Charpentier Avatar answered Oct 06 '22 01:10

Luc Charpentier


If you don't want to use milliseconds you can also use this function:

function AddDays (days) {
    var today = new Date();
    var resultDate = new Date(today);
    resultDate.setDate(today.getDate()+days);
    return resultDate;
}
like image 25
Pascal Avatar answered Oct 06 '22 02:10

Pascal