6
6

More than 5 years have passed since last update.

jQueryのCookieの有効期限を当日の深夜23:59:59までにする方法(要jQuery.cookie.js)

Last updated at Posted at 2016-06-20

jQuery.cookie.jsでは

$.cookie('submit_date', 'submitted' , { expires: 1 });

のような書き方をすればcookieの有効期限を1日後までに設定できますが、
これだと例えば1日1回まで入力を受け付けるフォームを作成しようと考えた時、
フォーム内容を投稿してから24時間待たないと次の投稿が出来ず、要件を満たせません。

そこで、フォーム内容を投稿した日の23:59:59で、cookieを破棄するスクリプトを下記します。

var date = new Date();
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
$.cookie('submit_date', 'submitted' , { expires: midnight });

取り急ぎ。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6