How can I set a cookie with expire time?
I've set the time to 1000*36000.
function display() {
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*36000;
now.setTime(expireTime);
document.cookie = 'cookie=ok;expires='+now.toUTCString()+';path=/';
//console.log(document.cookie); // 'Wed, 31 Oct 2012 08:50:17 UTC'
}
How to get cookie's expire time
This is difficult to achieve, but the cookie expiration date can be set in another cookie. This cookie can then be read later to get the expiration date. Maybe there is a better way, but this is one of the methods to solve your problem.
How to get cookie expiration date / creation date from javascript?
The information is not available through document.cookie, but if you're really desperate for it, you could try performing a request through the XmlHttpRequest object to the current page and access the cookie header using getResponseHeader().
Reading cookie expiration date
It is not possible to get the expiration date of a cookie through Javascript; only key-value pairs are exposed through document.cookie
.
Cookie expiration time
Your example code sets the cookie to expire 315532800000 milliseconds from now, i.e. that resolves to 3652 days or just about 10 years into the future.
jscookie.js how to expire cookie in minutes not days?
Found the answer in:
Frequently-Asked-Questions
JavaScript Cookie supports a Date instance to be passed in the expires
attribute. That provides a lot of flexibility since a Date instance
can specify any moment in time.Take for example, when you want the cookie to expire 15 minutes from
now:
var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {
expires: inFifteenMinutes
});
Also, you can specify fractions to expire in half a day (12 hours):
Cookies.set('foo', 'bar', {
expires: 0.5
});
Or in 30 minutes:
Cookies.set('foo', 'bar', {
expires: 1/48
});
Setting cookie expiry time
Just don't set a static expiration date in your cookie but a dynamic one:
var now = new Date(); //get the current date
now.setMonth(now.getMonth() + 1); //add one month to it
document.cookie = "ModalShown=true; expires=" + now.toUTCString() + "; path=/";
How to get this cookie to expire after 14 days
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();
}
jQuery cookie expiry time
The plugins expires
option accepts either a number or a date object.
If a number is passed, it's the number of days until the cookie expires, but if a date object is passed, it's the time and date when the cookie expires, so you can do
var expDate = new Date();
expDate.setTime(expDate.getTime() + (15 * 60 * 1000)); // add 15 minutes
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: expDate });
Related Topics
Difference Between Eloquent\Model::Get() and All()
How Would I Stop This Foreach Loop After 3 Iterations
Detect In-App Browser (Webview) with PHP/Javascript
When and Why Should $_Request Be Used Instead of $_Get/$_Post/$_Cookie
Php-Sort Array Based on Another Array
Why I am Getting Error When Getting Booking from Database
Php: Regular Expression to Get a Url from a String
PHP Exec: Does Not Return Output
Php: Limit Foreach() Statement
Using If(!Empty) with Multiple Variables Not in an Array