0
1

More than 3 years have passed since last update.

JavaScript 学習記録 #1 Dateオブジェクト

Posted at
1 / 2

わかりやすく日時を表示する方法:point_up:コードの書き方

①プログラムを書く準備

date.js
<section>
    <p>最終アクセス日時<span id="time"></span></p>
</section>

最終アクセス日時:

②「年/月/日 時:分」のかたちで表示
 そのためには「年、月、日、時、日、分」を個別に取得する必要がある。

date.js
<script>
'use strict';

const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const date = now.getDate();
const hour = now.getHours();
const min = now.getMinutes();

const output = `${year}/${month + 1}/${date} ${hour}:${min}`;
document.getElementById('time').textContent = output;
</script>

③Dateオブジェクト
 日時を扱うためのオブジェクトであり、次のようなことができる
  1.現在日時を取得する
  2.過去や未来の日時を設定する
  3.日時の計算をする

 :point_right:Dateオブジェクトは初期化(インスタンス化)する必要がある
   new Date();
  ※いくつかのオブジェクトは、newを使って初期化してから使う
 :point_right:月を取得するgetMonthメソッドは「実際の月-1」の数字が取得されるため、+1を忘れずに
  20200516_200516_0002.jpg

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