3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Memo]javascript-日付差分計算

Last updated at Posted at 2017-06-30

・javascriptでcookieの有効期限を設定するときにjquery.cookieを使用していたので日数設定しかできず
 その際に作成したfunctionをメモ

##コード

    var getDateDiff = function(dateStartStr,dateEndStr){

	  var dateStart = new Date(dateStartStr);
	  var dateEnd = new Date(dateEndStr);

	  //ミリ秒に変換
	  var msDiff = dateEnd.getTime() - dateStart.getTime();

	  // 経過ミリ秒÷(1000ミリ秒×60秒×60分×24時間) 端数切捨て
	  var daysDiff = Math.floor(msDiff / (1000 * 60 * 60 *24));

	  // 計算した差分に1加算してreturn
	  return ++daysDiff;
    };

##その他
  こうしたほうがいいとかあったら教えてください。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?