0
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.

40 代おっさん GASのDateオブジェクトについて学ぶ

Posted at

本記事ついて

本記事は プログラミング初学者の私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。

Dateオブジェクト

日付、時刻を取り扱うオブジェクト

構文

new Date()

指定の日時でインスタンスを生成するのであれば

new Date(, , [, , , , ミリ秒])

「set 05 2022 23:20:15」といった英文形式を指定することも可能

new Date(日時文字列)

1970/01/01 00:00:00からの経過ミリ秒をタイムスタンプ値と言う
それを指定することもできる

new Date(タイムスタンプ値)

Dateオブジェクトに変更を加える櫃王があり、かつ、もとのDateオブジェクトものこしておきたいときには

new Date(Dateオブジェクト)

お試し

function toshiki() {
  console.log(new Date());
  console.log(new Date(2022, 2, 18, 11, 30, 15 ));
  console.log(new Date('2022/02/18 11:30:15'));
  console.log(new Date(1645151415));

  const d = new Date();
  console.log(new Date(d));
}
分類 メンバー 戻り値 説明
メソッド getTullYear() Integer 年(4桁までの年)を返す
メソッド getMonth() Integer 月(0-11)を返す
メソッド getDate() Integer 日(1-31)を返す
メソッド getDay() Integer 曜日(0-6)を返す
メソッド getHours() Integer 時(0-23)を返す
メソッド getMinutes() Integer 分(0-59)を返す
メソッド getSeconds() Integer 秒(0-59)を返す
メソッド getMilliseconds() Integer ミリ秒(0-999)を返す
メソッド getTime() Integer 1970年1月1日00:00:00からの経過ミリ秒を返す
メソッド getTimezoneOffset() Integer 現地の時刻と協定世界時刻(UTC)との差を返す
メソッド setFullYear(y) Integer 年をy(4桁までの年)に設定する
メソッド setMonth(m) Integer 月をm(0-11)に設定する
メソッド setDate(d) Integer 日をd(1-31)に設定する
メソッド setHours(h) Integer 時をh(0-23)に設定する
メソッド setMinutes(m) Integer 分をm(0-59)に設定する
メソッド setSeconds(s) Integer 秒をs(0-59)に設定する
メソッド setMilliseconds(ms) Integer ミリ秒をms(0-999)に設定する
メソッド setTime(ts) Integer 1970年1月1日00:00:00からの経過ミリ秒をtsに設定する
メソッド toString() String 日時を文字列に変換したものを返す
メソッド toDateString() String 日付部分を文字列に変換したものを返す
メソッド toTimeString() String 時刻部分を文字列に変換したものを返す
メソッド toJSON() String 日時をJSON文字列に変換したものを返す

お試し

function toshiki2() {
  const d = new Date(2022, 2, 19, 14, 38, 15, 555);

  console.log(d.getFullYear());
  console.log(d.getMonth());
  console.log(d.getDate());
  console.log(d.getDay());
  console.log(d.getHours());
  console.log(d.getMinutes());
  console.log(d.getSeconds());
  console.log(d.getMilliseconds());
  console.log(d.getTime());
  console.log(d.getTimezoneOffset());

  d.setFullYear(2022);
  d.setMonth(0);
  d.setDate(1);
  d.setHours(1);
  d.setMinutes(11);
  d.setSeconds(11);
  d.setMilliseconds(111);

  console.log(d.toString());
  console.log(d.toDateString());
  console.log(d.toTimeString());
  console.log(d.toJSON());
}

日時の演算と複製

Dateオブジェクトには日時の演算をするメソッドは用意されていない。
要素を取り出し計算を行う。

function toshiki2() {
  const start = new Date('2022/2/19 14:00');
  const end   = new Date(start);

  end.setMinutes(start.getMinutes() + 120);
  console.log(start);
  console.log(end);
}

参考資料

https://www.amazon.co.jp/s?k=google+apps+script+%E5%AE%8C%E5%85%A8%E5%85%A5%E9%96%80&adgrpid=110264232688&gclid=CjwKCAiA9aKQBhBREiwAyGP5lSl7AJJLCvOEHb4wQgMlyqW1fll5X8GDTT_Rkd1_soUAyIPMXQr26hoClHEQAvD_BwE&hvadid=553833563682&hvdev=c&hvlocphy=1009076&hvnetw=g&hvqmt=b&hvrand=4378489642044417389&hvtargid=kwd-594191211348&hydadcr=4106_13159878&jp-ad-ap=0&tag=googhydr-22&ref=pd_sl_2x1owglv0s_b_p52

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