如何在午夜使用Jquery使Cookie过期?

我这样做了:

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

但它只会在第二天到期。

我怎样才能在午夜过期?

这会改变吗?

 var date = new Date(); var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); $.cookie("ultOS", (i), {expires: midnight}); 

我认为这会奏效:

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

根据最新版本的cookie插件(假设这是你正在使用的那个: http : //plugins.jquery.com/project/Cookie ),你可以传入一个普通的Date对象。

我没有尝试过,但插件的来源相当简单….

 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 } 

如果您传入一个数字,它会假定该天数。 如果您传入日期,则需要这样做。

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

您可以使用tonights(午夜)值创建Javascript Date对象,然后按如下方式设置到期时间:

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

其中date是日期对象。