LoginSignup
0
0

GASにおける日付比較メモ

Posted at

GASにおける日付比較メモ

自分でググってもわかりにくい解説が多かったのでシンプルメモ

キモの部分

1日は「1000 * 60 * 60 * 24」である
1秒 = 1000
1分 = 1000 x 60秒
1時間 = 1000 x 60秒 x 60分
1日 = 1000 x 60秒 x 60分 x 24時間

getTime

実際のコード

test.js
function DayCheck(){
	// スプレッドシート取得
	const ss =SpreadsheetApp.getActiveSpreadsheet();
	// シート取得
	const sf = ss.getSheetByName('シート1');
    // セルから日付取得
	var daycell = sf.getRange(1,1).getValues();
    // 1日
    const days = 1000 * 60 * 60 * 24;
    // 今日の日付
    var today_ = new Date();
    // 今日の日付とセルの日付を比較
    var diff = today_.getTime() - daycell.getTime();
    // 1日以上差がある場合
    if( today_.getTime() - daycell.getTime() > days ){
        console.log( "1日以上差があるよ!");
    }
}
0
0
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
0
0