JavaScriptの学習・参考リファレンス/ドキュメント

JavaScript、ECMAScriptの学習と参考メモ 入門~初心者~中級者~上級者を目指して

Date 日付日時

JavaScriptで日時はDate型で取り扱うことができます。

基本的な使い方

日時・日付の宣言

new Dateで返却される日時は地方時となります。例えば日本だと GMT+0900です。

let now = new Date(); // 現在の日時
let dateFromMilliseconds = new Date(1577804400000); // 2020/01/31 00:00:00 GMT+0900 1970年1月1日0時からの経過ミリ秒
let date2020_0101 = new Date(2020, 0); // 2020/01/01 00:00:00 GMT+0900 [note] 月の指定は0起算(0 -> 1月)
let date2020_0331 = new Date(2020, 2, 31); // 2020/03/31 00:00:00 GMT+0900
let date2020_0331_1500 = new Date(2020, 2, 31, 15); // 2020/03/31 15:00:00 GMT+0900
let date2020_0331_1530 = new Date(2020, 2, 31, 15, 30); // 2020/03/31 15:30:00 GMT+0900
let date2020_0331_1530_59 = new Date(2020, 2, 31, 15, 30, 59); // 2020/03/31 15:30:59 GMT+0900
let date2020_0331_1530_59_123 = new Date(2020, 2, 31, 15, 30, 59,123); // 2020/03/31 15:30:59.123 GMT+0900

2つの時刻間の期間

Date型の差は期間をミリ秒の整数で示したものとなります。

let begin = Date.now();
setTimeout(function(){
  let end = Date.now();
  let span = end - begin;
  console.log(span); // ex. 1009
},1000);

Dateのメソッド

協定世界時 (UTC) から現在時刻までの経過ミリ秒

let now = Date.now(); // ex. 1592107105873

文字列から日付型への変換

Date.parseにより文字列を協定世界時からの経過ミリ秒に変換できます。 タイムゾーンが未指定の場合は地方時として設定が行われます。

let date2020_0331 = Date.parse("2020-03-31"); // 1585612800000 -> 2020/03/31 09:00:00 GMT+0900
let date2020_0331_1530_59 = Date.parse("2020-03-31T15:30:59"); // 1585636259000 -> 2020/03/31 15:30:59 GMT+0900
let date2020_0331_1530_59Z = Date.parse("2020-03-31T15:30:59Z"); // 1585668659000 -> 2020/04/01 00:30:59 GMT+0900
let date2020_0331_1530_59P0 = Date.parse("2020-03-31T15:30:59+00:00"); // 1585668659000 -> 2020/04/01 00:30:59 GMT+0900
let date2020_0331_1530_59P9 = Date.parse("2020-03-31T15:30:59+09:00"); // 1585636259000 -> 2020/03/31 15:30:59 GMT+0900
let date2020_0331_1530_59_123Z = Date.parse("2020-03-31T15:30:59.123Z"); // 1585668659123 -> 2020/04/01 00:30:59.123 GMT+0900
let date2020_0331_1530_59_123P0 = Date.parse("2020-03-31T15:30:59.123+00:00"); // 1585668659123 -> 2020/04/01 00:30:59.123 GMT+0900
let date2020_0331_1530_59_123P9 = Date.parse("2020-03-31T15:30:59.123+09:00"); // 1585636259123 -> 2020/03/31 15:30:59.123 GMT+0900

協定世界時から指定した日時までの経過ミリ秒

new Dateと異なり、協定世界時からの経過ミリ秒を整数で取得する。

let date2020 = Date.UTC(2020); // 1577836800000 -> 2020/01/01 09:00:00 GMT+0900
let date2020_0101 = Date.UTC(2020, 0); // 1577836800000 -> 2020/01/01 09:00:00 GMT+0900
let date2020_0331 = Date.UTC(2020, 2, 31); // 1585612800000 -> 2020/03/31 09:00:00 GMT+0900
let date2020_0331_1500 = Date.UTC(2020, 2, 31, 15); // 1585666800000 -> 2020/04/01 00:00:00 GMT+0900
let date2020_0331_1530 = Date.UTC(2020, 2, 31, 15, 30); // 1585668600000 -> 2020/04/01 00:30:00 GMT+0900
let date2020_0331_1530_59 = Date.UTC(2020, 2, 31, 15, 30, 59); // 1585668659000 -> 2020/04/01 00:30:59 GMT+0900
let date2020_0331_1530_59_123 = Date.UTC(2020, 2, 31, 15, 30, 59,123); // 1585668659123 -> 2020/04/01 00:30:59.123 GMT+0900

日時の値を取得

Date型から日時の値を取得

let datetime = new Date(2020, 6, 1, 4, 30, 59,123); // 2020/07/01 04:30:59.123 GMT+0900

// 1593545459123 協定世界時(UTC)1970/01/01 00:00:00を起点とした正負ミリ秒
console.log(datetime.getTime());

//以下は地方時
console.log(datetime.getFullYear()); // 2020  4桁の年数
console.log(datetime.getMonth()); // 6 月(0起算)
console.log(datetime.getDate()); // 1 日
console.log(datetime.getHours()); // 4  時
console.log(datetime.getMinutes()); // 30 分
console.log(datetime.getSeconds()); // 59 秒
console.log(datetime.getMilliseconds()); // 123  ミリ秒
console.log(datetime.getDay()); // 3 地方時の曜日(0:日曜日, 1:月曜日, …, 6:土曜日)
console.log(datetime.getTimezoneOffset()); // -540 ロケールと協定世界時(UTC)の分単位での差分

//以下は協定世界時(UTC)
console.log(datetime.getUTCFullYear()); // 2020  4桁の年数
console.log(datetime.getUTCMonth()); // 5 月(0起算)
console.log(datetime.getUTCDate()); // 30 日
console.log(datetime.getUTCHours()); // 19  時
console.log(datetime.getUTCMinutes()); // 30 分
console.log(datetime.getUTCSeconds()); // 59 秒
console.log(datetime.getUTCMilliseconds()); // 123  ミリ秒
console.log(datetime.getUTCDay()); // 2 地方時の曜日(0:日曜日, 1:月曜日, …, 6:土曜日)

日時の値を設定

Date型から日時の値を変更する

let datetime = new Date();

//以下は地方時
datetime.setFullYear(2020);
datetime.setMonth(6); // 6は7月のこと
datetime.setDate(1);
datetime.setHours(4);
datetime.setMinutes(30);
datetime.setSeconds(59);
datetime.setMilliseconds(123);
console.log(datetime); // 2020/07/01 04:30:59.123 GMT+0900

//以下は協定世界時(UTC)
datetime.setUTCFullYear(2020);
datetime.setUTCMonth(6); // 6は7月のこと
datetime.setUTCDate(1);
datetime.setUTCHours(4);
datetime.setUTCMinutes(30);
datetime.setUTCSeconds(59);
datetime.setUTCMilliseconds(123);
console.log(datetime); // 2020/07/01 13:30:59.123 GMT+0900

//以下は協定世界時(UTC)
datetime.setTime(1585612800000); // 協定世界時(UTC)1970/01/01 00:00:00を起点とした正負ミリ秒
console.log(datetime); // 2020/03/31 09:00:00.000 GMT+0900

日時に関する情報の取得

日時のプリミティブ値

let datetime = new Date(2020, 6, 1, 4, 30, 59,123); // 2020/07/01 04:30:59.123 GMT+0900

// プリミティブ値
console.log(datetime.valueOf()); // 1593545459123 協定世界時(UTC)1970/01/01 00:00:00を起点とした正負ミリ秒

日時の文字列情報

let datetime = new Date(2020, 6, 1, 4, 30, 59,123); // 2020/07/01 04:30:59.123 GMT+0900

// 各文字列
console.log(datetime.toISOString()); // "2020-06-30T19:30:59.123Z" ISO8601形式での文字列を取得 [1]
console.log(datetime.toJSON()); // "2020-06-30T19:30:59.123Z" 日付を示すシリアライズした文字列
console.log(datetime.toString()); // "Wed Jul 01 2020 04:30:59 GMT+0900 (日本標準時)"
console.log(datetime.toDateString()); // "Wed Jul 01 2020"
console.log(datetime.toTimeString()); // "04:30:59 GMT+0900 (日本標準時)"
console.log(datetime.toUTCString()); // "Tue, 30 Jun 2020 19:30:59 GMT"

ロケール(国・地域・言語)と表示形式に対応した文字列

以下各メソッドで、表示形式を指定した文字列の情報は取得を行えます。

toLocaleString(locales, options)
toLocaleDateString(locales, options)
toLocaleTimeString(locales, options)

localesは、言語コード、国・地域コード、Unicode拡張等をハイフンで連結した文字列です(参考:language-subtag-registry )。
例えばja-JP-u-ca-japaneseは以下から構成されています。

  • ja 日本語
  • JP 日本
  • u-ca-japanese 和暦

optionsは以下が指定可能です(ECMAScript Internationalization API Specification – ECMA-402 Edition 1.0)。

optionsのプロパティ名称 指定可能な値
weekday “narrow”, “short”, “long”
era “narrow”, “short”, “long”
year “2-digit”, “numeric”
month “2-digit”, “numeric”, “narrow”, “short”, “long”
day “2-digit”, “numeric”
hour “2-digit”, “numeric”
minute “2-digit”, “numeric”
second “2-digit”, “numeric”
timeZoneName “short”, “long”

具体的には次のようになります。

let datetime = new Date(2020, 6, 1, 4, 30, 59,123); // 2020/07/01 04:30:59.123 GMT+0900

// ロケールを指定可能な 日時
console.log(datetime.toLocaleString()); // "2020/7/1 4:30:59"
console.log(datetime.toLocaleString('ja')); // "2020/7/1 4:30:59"
console.log(datetime.toLocaleString('ja-JP')); // "2020/7/1 4:30:59"
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese')); // "R2/7/1 4:30:59"
console.log(datetime.toLocaleString('en-US')); // "7/1/2020, 4:30:59 AM"
console.log(datetime.toLocaleString('en-GB')); // "01/07/2020, 04:30:59"

let options1 = { weekday: 'long', era:'long', year: '2-digit', month: 'long', day: '2-digit', hour:'2-digit', minute:'2-digit', second:'2-digit', timeZoneName:'long' };
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', options1)); // "令和02年7月01日水曜日 04時30分59秒 日本標準時"
console.log(datetime.toLocaleString('en-US', options1)); // "Wednesday, July 01, 20 Anno Domini, 04:30:59 AM Japan Standard Time"
console.log(datetime.toLocaleString('en-GB', options1)); // "Wednesday, 01 July 20 Anno Domini, 04:30:59 Japan Standard Time"

let options2 = { weekday: 'short', era:'short', year: 'numeric', month: 'short', day: 'numeric', hour:'numeric', minute:'numeric', second:'numeric', timeZoneName:'short' };
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', options2)); // "令和2年7月1日(水) 4:30:59 JST"
console.log(datetime.toLocaleString('en-US', options2)); // "Wed, Jul 1, 2020 AD, 4:30:59 AM GMT+9"
console.log(datetime.toLocaleString('en-GB', options2)); // "Wed, 1 Jul 2020 AD, 4:30:59 GMT+9"

let options3 = { weekday: 'narrow', era:'narrow', year: 'numeric', month: 'narrow', day: 'numeric', hour:'numeric', minute:'numeric', second:'numeric', timeZoneName:'short' };
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', options3)); // "R2年7月1日(水) 4:30:59 JST"
console.log(datetime.toLocaleString('en-US', options3)); // "W, J 1, 2020 A, 4:30:59 AM GMT+9"
console.log(datetime.toLocaleString('en-GB', options3)); // "W, 1 J 2020 A, 4:30:59 GMT+9"

//曜日だけの指定も可能
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {weekday: 'long'})); // "水曜日"
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {weekday: 'short'})); // "水"
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {weekday: 'narrow'})); // "水"

//元号を指定し先頭文字だけ切り出し
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {era: 'long'}).substring(0,2)); // "令和"
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {era: 'short'}).substring(0,2)); // "令和"
console.log(datetime.toLocaleString('ja-JP-u-ca-japanese', {era: 'narrow'}).substring(0,1)); // "R"

// 日付
console.log(datetime.toLocaleDateString()); // "2020/7/1"
console.log(datetime.toLocaleDateString('ja-JP-u-ca-japanese', {era:'long', year: '2-digit', month: 'long', day: '2-digit'})); // "令和02年7月01日"
console.log(datetime.toLocaleDateString('en-US', {year: 'numeric', month: 'long', day: '2-digit'})); // "July 01, 2020"
console.log(datetime.toLocaleDateString('en-GB', {year: 'numeric', month: 'long', day: '2-digit'})); // "01 July 2020"

// 時刻
console.log(datetime.toLocaleTimeString()); // "4:30:59"
console.log(datetime.toLocaleTimeString('ja-JP-u-ca-japanese', {hour:'2-digit', minute:'2-digit', second:'2-digit'})); // "04:30:59"
console.log(datetime.toLocaleTimeString('en-US', {hour:'2-digit', minute:'2-digit', second:'2-digit', hour12: false})); // "04:30:59" [note] hour12の指定がないと末尾に"AM"が表示
console.log(datetime.toLocaleTimeString('en-GB', {hour:'2-digit', minute:'2-digit', second:'2-digit'})); // "04:30:59"
作成日 : 2020年06月14日