JavaScript 現在時刻を取得する
- 2020.07.21
- javascript
- javascript

JavaScriptでローカル時刻の現在時刻を取得するサンプルコードを記述してます。 dateオブジェクトのtoLocaleStringを利用します。
環境
- OS windows10 pro 64bit
現在時刻取得
dateオブジェクトのtoLocaleStringを利用します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>mebee テスト</title> </head> <body> <div id="content"></div> <script> document.getElementById('content').innerHTML = new Date().toLocaleString(undefined, { year:"numeric", month: "short", day: "numeric", hour: "numeric", //2桁表示 minute: "2-digit", //12時間表示 hour12: true }); </script> </body> </html> |
実行結果は下記の通りとなります。

プロパティに何も指定しない場合は、下記と同じように全て「 numeric 」となります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>mebee テスト</title> </head> <body> <div id="content"></div> <script> document.getElementById('content').innerHTML = new Date().toLocaleString(undefined, { year:"numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", //12時間表示 hour12: false }); </script> </body> </html> |
実行結果

-
前の記事
ruby エラー「Unable to load the EventMachine C extension; To use the pure-ruby reactor, require ‘em/pure_ruby’」発生時の対処法 2020.07.21
-
次の記事
windows10 Ruby on Railsをインストールして利用する手順 2020.07.21
コメントを書く