1
1

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 3 years have passed since last update.

GASで日付を足したり減らしたりする

Last updated at Posted at 2020-12-03

単純に

const today = new Date();
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);

で良いそうです(コメント参照)。
難しく考えすぎでした。


GASではDate型のオブジェクトが使えますが、このオブジェクトにはaddDateのような関数は用意されていません。
そのため、「(実行時から見て)先週の日曜日を求めたい」というような場合に困ります。
そんな時はsetTimeを使います。

// 昨日を求める
const today = new Date();
const yesterdayMillis = today.getTime() - (24 * 60 * 60 * 1000);
const yesterday = new Date();
yesterday.setTime(yesterdayMillis);

つまり、加算・減算したい分のミリ秒を作ってあげて、その結果をsetTimeしてあげるということです。

1
1
2

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?