2011-01-07 3 views
8

私はこれでした:真夜中にJqueryを使用してクッキーを期限切れにする方法は?

$.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}); 
+0

これは非常にスマートです!私はdate.getDate()+ 1を使用するつもりでしたが、以下のように答えられましたが、月末に関係していました。あなたの深夜は完璧な真夜中です! –

答えて

9

私はこれが仕事だと思う:

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

私はあなたが答えたときに質問を編集していました。タックス – Thiago

3

THSクッキープラグインの最新バージョン(これはあなたが使用しているものであると仮定: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 
     } 

数字を渡すと、それは日数とみなされます。あなたが日付を渡すなら、それはそれを取る。

0

あなたは次のように有効期限を設定し、今夜(深夜)値でJavascriptのDateオブジェクトを作成することができます。日付は日付オブジェクトである

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

を。

1
var date = new Date(); 
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59); 
var expires = "expires="+midnight.toGMTString(); 
関連する問題