現在の時刻を取得する - JavaScript

JavaScriptで現在時刻を取得するコードを紹介します。

概要

JavaScriptで現在の時刻を表示する場合は、new Date()にてDateオブジェクトを作成し、 getHours() getMinutes() メソッドなどを呼び出すことで時刻や日付の要素を取得できます。

また、今日の日付を表示する場合は、new Date()にてDateオブジェクトを作成し、 getFullYear() getMonth() getDate()メソッドなどを呼び出すことで日付の要素を取得できます。

書式

Dateオブジェクトの作成

Dateオブジェクトのインスタンスを作成すると、現在の時刻を示すDateオブジェクトが作成されます。
var (変数名) = new Date();

各日時要素の取得

Dateオブジェクトが示す時刻や日付の要素を取得するには下記のメソッドを利用します。

項目メソッド名
getFullYear()
getMonth()
getDate()
getHours()
getMinutes()
getSeconds()
ミリ秒getMilliseconds()
曜日getDay()
注意:月の表現
getDay()で取得できる月の数値の意味は下記になります。getMonth()で取得できる月の値はその数値が示す月ではありませんので注意してください。
getMonth()の取得値
getMonth()の値意味
0January1月
1February2月
2March3月
3April4月
4May5月
5June6月
6July7月
7August8月
8September9月
9October10月
10Nobember11月
11December12月

曜日の表現

getDay()で取得できる曜日の数値の意味は下記になります。

getDay()の取得値
getDay()の値意味曜日
0Sunday日曜日
1Monday月曜日
2Tuesday火曜日
3Wednesday水曜日
4Thursday木曜日
5Friday金曜日
6Saturday土曜日

プログラム例1:現在の時刻の取得

コード

以下のHTMLファイルを作成します。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
  <script type="text/javascript">
    function LoadProc() {
      var now = new Date();
      var YearValue = now.getFullYear();
      var MonthValue = now.getMonth()+1;
      var DateValue = now.getDate();
      var HourValue = now.getHours();
      var MinValue = now.getMinutes();
      var SecValue = now.getSeconds();

      var target = document.getElementById("DateTimeDisp");
      target.innerHTML = YearValue + "年" + MonthValue + "月" + DateValue + "日" + HourValue + ":" + MinValue + ":" + SecValue;
    }

  </script>
</head>
<body onload="LoadProc();">
現在の時刻:
   <div id="DateTimeDisp"></div>
</body>
</html>

解説

Dateオブジェクトを新規に作成し、"DateTimeDisp"のIDを持つDIVタグの要素を取得し現在の時刻を表示します。
getMonth()は1月が0、12月が11となるため、1を加算することに注意してください。
function LoadProc() {
  var now = new Date();
  var YearValue = now.getFullYear();
  var MonthValue = now.getMonth()+1;
  var DateValue = now.getDate();
  var HourValue = now.getHours();
  var MinValue = now.getMinutes();
  var SecValue = now.getSeconds();

  var target = document.getElementById("DateTimeDisp");
  target.innerHTML = YearValue + "年" + MonthValue + "月" + DateValue + "日" + HourValue + ":" + MinValue + ":" + SecValue;
}
補足
Bodyタグ内の onload="LoadProc();" を忘れずに。

実行結果

HTMLファイルを表示します。下図の画面が表示されます。現在の時刻が表示されます。


ページをリロードすると時刻表示も更新されます。

プログラム例2:今日の日付(現在の日付)の取得

今日の日付を取得する場合のコードです。

コード

以下のHTMLファイルを作成します。
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <script type="text/javascript">
    function LoadProc() {
      var now = new Date();

      var YearValue = now.getFullYear();
      var MonthValue = now.getMonth()+1;
      var DateValue = now.getDate();

      var target = document.getElementById("DateTimeDisp");
      target.innerHTML = YearValue + "年" + MonthValue + "月" + DateValue + "日";
    }
  </script>

</head>
<body onload="LoadProc();">
  <h2>今日の日付を取得するデモ</h2>
  <div>
    今日の日付:<span id="DateTimeDisp"></span>
  </div>
</body>
</html>

解説

ページロード時に以下の LoadProc() 関数を呼び出し実行します。 Dateオブジェクトを作成しgetFullYear() getMonth() getDate() メソッドを呼び出し、今日の日付の要素の値を取得し、 画面に表示します。
    function LoadProc() {
      var now = new Date();

      var YearValue = now.getFullYear();
      var MonthValue = now.getMonth()+1;
      var DateValue = now.getDate();

      var target = document.getElementById("DateTimeDisp");
      target.innerHTML = YearValue + "年" + MonthValue + "月" + DateValue + "日";
    }

実行結果

HTMLファイルをWebブラウザで表示します。下図のページが表示されます。
実行時の日付がページに表示されることが確認できます。

変換に関する注意事項
下記コードは月数を取得し1を加算するコードです。
  var MonthValue = now.getMonth()+1;

明示的に数値として指定する場合は、下記のコードで数値であることを明示的に指定できます。
  var MonthValue = Number(now.getMonth())+1;

一方で、文字列を整形する下記のコードで1を加算する処理を記述すると、文字として認識されて期待した結果と異なる出力になる場合があります。
 target.innerHTML = YearValue + "年" + MonthValue+1 + "月" + DateValue + "日" + HourValue + ":" + MinValue + ":" + SecValue;

上記の出力結果
2017年111月16日15:30:24
Monthの値 "11" が数値ではなく文字列と認識されるため、 Month+1 が "111" の結果になってしまいます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2013-06-10
改訂日: 2023-12-22
iPentec all rights reserverd.