parse【日時文字列解析】
toString【文字列変換】
toDateString【日付文字列変換 (ローカル時刻)】
toTimeString【時刻文字列変換 (ローカル時刻)】
toLocaleString【ロケール日時文字列変換 (ローカル時刻)】
toLocaleDateString【ロケール日付文字列変換 (ローカル時刻)】
toLocaleTimeString【ロケール時刻文字列変換 (ローカル時刻)】
toGMTString【日時文字列変換 (GMT)】
toISOString【日時文字列変換 (ISO)】
toJSON【JSON テキスト変換】
toUTCString【日時文字列変換 (UTC)】


文字列 (日時文字列フォーマット) ⇔ 日時 (その他変換)
文字列 ⇒ 経過ミリ秒
日時文字列 解析 parse
文字列・経過ミリ秒・個別指定・現在日時 ⇒ 日時
コンストラクタ new Date
日時 ⇒ 文字列
UTCローカル時刻 (上段)
ロケール (下段)
ISOJSONGMT
日時文字列 変換 toUTCString toString (実装依存) toISOString toJSON toGMTString
toLocaleString
(参考)
Intl.DateTimeFormat
日付文字列 変換toDateString (実装依存)
toLocaleDateString
時刻文字列 変換toTimeString (実装依存)
toLocaleTimeString
現在日時文字列 変換Date【型変換】
―


Date.parse【日時文字列解析】

メモ

概要

  • 日時文字列を解析し、1970/01/01 00:00:00 UTC からの経過ミリ秒を取得
    • この値で Date【日時】オブジェクトの生成可
  • 日時文字列フォーマット (YYYY-MM-DDTHH:mm:ss.sssZ) の省略時の仕様
    • MM または DD 省略:01
    • HH または mm または ss 省略:00
    • sss 省略:000
    • タイムゾーン省略:日付のみはUTC時刻、日付+時刻はローカル時刻

関連

外部リンク

構文

Date.parse( string ) 

1970/01/01 00:00:00 UTC からの経過ミリ秒 (NaN:解析不可)
string日時文字列
日時文字列フォーマット
上記以外の日時文字列 (実装依存)

例

let time = Date.parse("2001");
let date = new Date(time);
console.log(date.toISOString());  // 出力:2001-01-01T00:00:00.000Z

time = Date.parse("2001-02");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-01T00:00:00.000Z

time = Date.parse("2001-02-03");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-03T00:00:00.000Z

time = Date.parse("2001-02-03T04:05");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:00.000Z
time = Date.parse("2001-02-03T04:05+09:00");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:00.000Z
time = Date.parse("2001-02-03T04:05Z");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-03T04:05:00.000Z

time = Date.parse("2001-02-03T04:05:06");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:06.000Z
time = Date.parse("2001-02-03T04:05:06+09:00");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:06.000Z
time = Date.parse("2001-02-03T04:05:06Z");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-03T04:05:06.000Z

time = Date.parse("2001-02-03T04:05:06.078");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:06.078Z
time = Date.parse("2001-02-03T04:05:06.078+09:00");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-02T19:05:06.078Z
time = Date.parse("2001-02-03T04:05:06.078Z");
date = new Date(time);
console.log(date.toISOString());  // 出力:2001-02-03T04:05:06.078Z

time = Date.parse("+123456-07-08T09:10:11.012");
date = new Date(time);
console.log(date.toISOString());  // 出力:+123456-07-08T00:10:11.012Z
time = Date.parse("+123456-07-08T09:10:11.012+09:00");
date = new Date(time);
console.log(date.toISOString());  // 出力:+123456-07-08T00:10:11.012Z
time = Date.parse("+123456-07-08T09:10:11.012Z");
date = new Date(time);
console.log(date.toISOString());  // 出力:+123456-07-08T09:10:11.012Z

time = Date.parse("");
console.log(time);  // 出力:NaN

Date.prototype.toString【文字列変換】

メモ

概要

  • 日時を文字列に変換 (実装依存)
  • ユーザ作成フォーマット関数
    • 汎用的なフォーマットメソッドがない為、該当書式が既存のメソッドにない場合、作成が必要
      (下記のテンプレートリテラル を使用した例を参照)

関連

外部リンク

構文

date.toString()

日時の表現文字列 (実装依存)
"Invalid Date" (無効な値) 

例

var date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toString());                             // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))
// 参考
console.log(date.toGMTString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());                          // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());                               // 出力:2001-02-02T19:05:06.789Z
console.log(date.toLocaleString());                       // 出力:2001/2/3 4:05:06
console.log(date.toLocaleString("ja-JP-u-ca-japanese"));  // 出力:平成13/2/3 4:05:06
console.log(date.toUTCString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT
// フォーマット関数 (ゼロパディング)
function format(dateTime) {
  const year = dateTime.getFullYear();
  const month = 1 + dateTime.getMonth();
  const month2 = (month < 10) ? "0" + month : month;
  const date = dateTime.getDate();
  const date2 = (date < 10) ? "0" + date : date;
  const hour = dateTime.getHours();
  const hour2 = (hour < 10) ? "0" + hour : hour;
  const min = dateTime.getMinutes();
  const min2 = (min < 10) ? "0" + min : min;
  const sec = dateTime.getSeconds();
  const sec2 = (sec < 10) ? "0" + sec : sec;
  const ms = dateTime.getMilliseconds();
  const ms2 = ("00" + ms).slice(-3);
  const days = ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'];
  const day = dateTime.getDay();
  
  return `${year}/${month2}/${date2} ${hour2}:${min2}:${sec2}.${ms2} (${days[day]})`
}

const array = [
  "2001-02-03T04:05:06.007",        // 出力:2001/02/03 04:05:06.007 (土曜日)
  "2001-02-03T04:05:06.078+09:00",  // 出力:2001/02/03 04:05:06.078 (土曜日)
  "2345-12-23T12:34:56.789",        // 出力:2345/12/23 12:34:56.789 (日曜日)
  "2001-02-03",                     // 出力:2001/02/03 09:00:00.000 (土曜日)
  "2001-02-03T00:00"                // 出力:2001/02/03 00:00:00.000 (土曜日)
];

for (let i = 0; i < array.length; i++) {
  const date = new Date(array[i]);
  console.log(format(date));
}

Date.prototype.toDateString【日付文字列変換 (ローカル時刻)】

メモ

概要

  • ローカル時刻の日付部分を文字列に変換 (実装依存:通常、アメリカ式表記)

関連

外部リンク

構文

date.toDateString()

ローカル時刻の日付部分の文字列 (実装依存)

例

var date = new Date("2001-02-03T04:05:06Z");
console.log(date.toDateString());       // 出力:Sat Feb 03 2001
console.log(date.toLocaleDateString()); // 出力:2001/2/3・2001‎年‎2‎月‎3‎日 等 (実装依存)

Date.prototype.toTimeString【時刻文字列変換 (ローカル時刻)】

メモ

概要

  • ローカル時刻の時刻文字列に変換 (実装依存)

関連

外部リンク

構文

date.toTimeString()

ローカル時刻の時刻文字列 (実装依存)

例

var date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toTimeString());                       // 出力:04:05:06 GMT+0900 (東京 (標準時))
// 参考
console.log(date.toLocaleTimeString());                 // 出力:4:05:06
var options = { timeZoneName:"short" };
console.log(date.toLocaleTimeString("ja-JP", options)); // 出力:4:05:06 GMT+9

Date.prototype.toLocaleString【ロケール日時文字列変換 (ローカル時刻)】

メモ

概要

  • ローカル時刻をロケール日時文字列に変換 (実装依存)

関連

外部リンク

構文

date.toLocaleString() 
date.toLocaleString( [reserved1[, reserved2]] ) 
date.toLocaleString( [locales[, options]] ) 

ローカル時刻のロケール日時文字列 (実装依存)
reserved1リザーブ
reserved2リザーブ
localesBCP 47 の言語タグの文字列 または その配列 (省略:デフォルトのロケール)
optionsオプション
参照:new Intl.DateTimeFormat()【国際化日時フォーマット コンストラクタ】

例

var date = new Date("2001-02-03T04:05:06.789+09:00");
// 参考
console.log(date.toGMTString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());                          // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());                               // 出力:2001-02-02T19:05:06.789Z
console.log(date.toString());                             // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT
// 出力は実装依存
console.log(date.toLocaleString());                       // 出力:2001/2/3 4:05:06
console.log(date.toLocaleString("ja-JP"));                // 出力:2001/2/3 4:05:06
console.log(date.toLocaleString("ja-JP-u-ca-japanese"));  // 出力:平成13/2/3 4:05:06・平成‎13‎年‎2‎月‎3‎日‎4‎:‎05‎:‎06 等
var options = { year:"numeric", month:"numeric", day:"numeric", weekday:"long" , hour:"numeric", minute:"numeric", second:"numeric", timeZoneName:"short"};
console.log(date.toLocaleString("ja-JP", options));       // 出力:2001/2/3土曜日 4:05:06 GMT+9

console.log(date.toLocaleString("en-US"));                // 出力:2/3/2001, 4:05:06 AM
options = { weekday:"long", month:"long", day:"numeric", year:"numeric", hour:"numeric", minute:"numeric", second:"numeric", timeZoneName:"short" };
console.log(date.toLocaleString("en-US", options));       // 出力:Saturday, February 3, 2001, 4:05:06 AM GMT+9

Date.prototype.toLocaleDateString【ロケール日付文字列変換 (ローカル時刻)】

メモ

概要

  • ローカル時刻の日付部分をロケールに基づいて文字列に変換 (実装依存)

関連

外部リンク

構文

date.toLocaleDateString() 
date.toLocaleDateString( [reserved1[, reserved2]] ) 
date.toLocaleDateString( [locales[, options]] ) 

ローカル時刻の日付部分の文字列 (実装依存)
reserved1リザーブ
reserved2リザーブ
localesBCP 47 の言語タグの文字列 または その配列 (省略:デフォルトのロケール)
optionsオプション
参照:new Intl.DateTimeFormat()【国際化日時フォーマット コンストラクタ】

例

var date = new Date("2001-02-03T04:05:06+09:00");
// 参考
console.log(date.toDateString());                             // 出力:Sat Feb 03 2001
// 出力は実装依存
console.log(date.toLocaleDateString());                       // 出力:2001/2/3
console.log(date.toLocaleDateString("ja-JP"));                // 出力:2001/2/3
console.log(date.toLocaleDateString("ja-JP-u-ca-japanese"));  // 出力:平成13/2/3・平成‎13‎年‎2‎月‎3‎日 等
var options = { year:"numeric", month:"numeric", day:"numeric", weekday:"long" };
console.log(date.toLocaleDateString("ja-JP", options));       // 出力:2001/2/3土曜日

Date.prototype.toLocaleTimeString【ロケール時刻文字列変換 (ローカル時刻)】

メモ

概要

  • ローカル時刻の時刻部分をロケールに基づいて文字列に変換 (実装依存)

関連

外部リンク

構文

date.toLocaleTimeString() 
date.toLocaleTimeString( [reserved1[, reserved2]] ) 
date.toLocaleTimeString( [locales[, options]] ) 

ローカル時刻の時刻文字列 (実装依存)
reserved1リザーブ
reserved2リザーブ
localesBCP 47 の言語タグの文字列 または その配列 (省略:デフォルトのロケール)
optionsオプション
参照:new Intl.DateTimeFormat()【国際化日時フォーマット コンストラクタ】

例

var date = new Date("2001-02-03T04:05:06.789+09:00");
// 参考
console.log(date.toTimeString());                       // 出力:04:05:06 GMT+0900 (東京 (標準時))
// 出力は実装依存
console.log(date.toLocaleTimeString());                 // 出力:4:05:06
console.log(date.toLocaleTimeString("en-US"));          // 出力:4:05:06 AM
var options = { timeZoneName:"short" };
console.log(date.toLocaleTimeString("ja-JP", options)); // 出力:4:05:06 GMT+9

Date.prototype.toGMTString【日時文字列変換 (GMT)】

メモ

概要

関連

外部リンク

構文

date.toGMTString() 

GMTの日時文字列

例

var date = new Date("2001-02-03T04:05:06.789Z");
console.log(date.toGMTString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-03T04:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-03T04:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 13:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 13:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT

date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toGMTString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-02T19:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 4:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT

Date.prototype.toISOString【日時文字列変換 (ISO)】

メモ

概要

  • ISO 8601拡張形式の日時文字列に変換 (タイムゾーンはUTC)
    • "YYYY-MM-DDTHH:mm:ss.sssZ"
    • "±YYYYYY-MM-DDTHH:mm:ss.sssZ" (拡大表記)

関連

外部リンク

構文

date.toISOString()

ISO 8601拡張形式の日時文字列
例外RangeError 例外
値が有限数以外
年が範囲外 

例

let date = new Date("2001-02-03T04:05:06.789Z");
console.log(date.toGMTString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-03T04:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-03T04:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 13:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 13:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT

date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toGMTString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-02T19:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 4:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT

date = new Date("+123456-07-08T09:00:01.234Z");
console.log(date.toISOString());    // 出力:+123456-07-08T09:00:01.234Z

date = new Date(NaN);
console.log(date.toISOString());    // RangeError 例外
date = new Date(+Infinity);
console.log(date.toISOString());    // RangeError 例外
date = new Date(-Infinity);
console.log(date.toISOString());    // RangeError 例外
date = new Date("+300000-00-00T00:00:00Z");
console.log(date.toLocaleString()); // 出力:Invalid Date
console.log(date.toISOString());    // RangeError 例外

Date.prototype.toJSON【JSON テキスト変換】

メモ

概要

関連

外部リンク

構文

date.toJSON( key )
date.toJSON( ) 

JSON テキスト
key無効 (実装依存)

例

var date = new Date("2001-02-03T04:05:06.789Z");
console.log(date.toGMTString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-03T04:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-03T04:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 13:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 13:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Sat, 03 Feb 2001 04:05:06 GMT

date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toGMTString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());    // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());         // 出力:2001-02-02T19:05:06.789Z
console.log(date.toLocaleString()); // 出力:2001/2/3 4:05:06
console.log(date.toString());       // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))
console.log(date.toUTCString());    // 出力:Fri, 02 Feb 2001 19:05:06 GMT

// JSON.stringify の内部から呼び出し
var date = new Date(2001, (2-1), 3, 9); // 日本時間 9:00
console.log(JSON.stringify(date));      // 出力:"2001-02-03T00:00:00.000Z"

Date.prototype.toUTCString【日時文字列変換 (UTC)】

メモ

概要

  • UTC(協定世界時)の日時文字列に変換 (実装依存)

関連

外部リンク

構文

date.toUTCString( )

UTCの日時文字列 (実装依存)

例

var date = new Date("2001-02-03T04:05:06.789+09:00");
console.log(date.toUTCString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT

// 参考
console.log(date.toGMTString());                          // 出力:Fri, 02 Feb 2001 19:05:06 GMT
console.log(date.toISOString());                          // 出力:2001-02-02T19:05:06.789Z
console.log(date.toJSON());                               // 出力:2001-02-02T19:05:06.789Z
console.log(date.toLocaleString());                       // 出力:2001/2/3 4:05:06
console.log(date.toLocaleString("ja-JP-u-ca-japanese"));  // 出力:平成13/2/3 4:05:06
console.log(date.toString());                             // 出力:Sat Feb 03 2001 04:05:06 GMT+0900 (東京 (標準時))