Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expire a Cookie using Jquery at midnight?

I did this:

$.cookie("ultOS", (i), {expires:1});

But it will only expire next day.

How can I expire a cookie at midnight?

Would this work instead?

var date = new Date();
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
$.cookie("ultOS", (i), {expires: midnight});
like image 483
Thiago Avatar asked Jan 07 '11 16:01

Thiago


People also ask

How do I set cookies to expire time?

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 cookies expire at end of session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.

What happen if cookie expires max age is session?

Using cookies to do stuff Cookies without an Expires or Max-Age attribute are treated as session cookies, which means they are removed once the browser is closed. Setting a value on either Expires or Max-Age makes them permanent cookies, since they will exist until they hit their expiry date.


1 Answers

I think this would work:

var currentDate = new Date();
expirationDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1, 0, 0, 0);
$.cookie("ultOS", "5", {expires: expirationDate});
like image 150
Adam Prax Avatar answered Oct 27 '22 06:10

Adam Prax