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.
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.
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.
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 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.
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.
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.
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.
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();
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With