How to end a cookie using jQuery at midnight?

I have done this:

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

But it will come only the next day.

How can I finish a cookie at midnight?

Will 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}); 
+8
jquery jquery-plugins
source share
4 answers

I think this will work:

 var currentDate = new Date(); expirationDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1, 0, 0, 0); $.cookie("ultOS", "5", {expires: expirationDate}); 
+9
source share

According to the latest version of the cookie plugin (assuming this is the one you are using: http://plugins.jquery.com/project/Cookie ), you can go through a regular Date object in.

I have not tried, but the source of the plugin is pretty simple.

 if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } 

If you pass a number, it assumes the number of days. If you pass a date, it takes it.

+3
source share
 var date = new Date(); var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); var expires = "expires="+midnight.toGMTString(); 
+1
source share

You can create a Javascript Date object with a value of tonights (midnight) and then set the expiration as follows:

  $.cookie("example", "foo", { expires: date }); 

Where date is a date object.

0
source share

Source: https://habr.com/ru/post/650804/


All Articles