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'
}
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
});
How to set a cookie to expire in 1 hour in Javascript?
Code :
var now = new Date();
var time = now.getTime();
time += 3600 * 1000;
now.setTime(time);
document.cookie =
'username=' + value +
'; expires=' + now.toUTCString() +
'; path=/';
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 add expiry time in hours for a cookie in Angular 2?
I had done like this, it worked for me.
Cookie.set('SESSION_TOKEN',userObject.sessionToken,0.25);
Actually if we give 1
it stays long for 24hrs, So I applied mathematical logic to it, i.e. for 6 hours.
1 day -> 24 hrs
? day -> 6 hrs
(1*6)/24 -> 0.25 i.e. 6hrs
Setting cookie to expire after one day
Change:
date.setTime(date.getTime() + 31536000000);
to:
date.setDate(date.getDate() + 1);
This adds 1 day to the date. The old code was adding 365 days.
Is it possible to set a cookie to expiry at the end of session and after 1 day?
the cookie will either expire on session end, or on a specific date, because in order to make the cookie expire on session end you actually omit the date to make it work.
unless you create 2 cookies one for session end and one on a date
the function below is from the page you posted. use it to calculate the date and set the cookie where exdays=1
. else just use the last line of the function omitting the expires
so it can expire on session end
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
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 "Process.Stdout.Write" and "Console.Log" in Node.Js
Tried to Load Angular More Than Once
How to Register Event with Useeffect Hooks
Can Any Desktop Browsers Detect When the Computer Resumes from Sleep
Why Is Bind Slower Than a Closure
How to Configure Different Environments in Angular.Js
Ipad/iPhone Browser Crashing When Loading Images in JavaScript
How to Replace the Entire HTML Node Using Jquery
Get Query String Parameters Url Values with Jquery/JavaScript (Querystring)
Changing the Default Title of Confirm() in JavaScript
Node.Js Async/Await Using with MySQL
Why Can't We Have Return in the Ternary Operator
Lodash Remove Duplicates from Array
Array.Prototype.Includes VS. Array.Prototype.Indexof
Date Parsing in JavaScript Is Different Between Safari and Chrome
How to Convert a String into a Math Operator in JavaScript
Using Lodash .Groupby. How to Add Your Own Keys for Grouped Output