LoginSignup
1
1

More than 1 year has passed since last update.

JavaScriptで使えるISO 8601の形式

Last updated at Posted at 2021-12-20

はじめに

ISO 8601は、日付と時刻の表記に関するISOの国際規格です。(wikipedia調べ)
JavaScriptのDateのコンストラクタ(およびDate.parse)に使用できる日時の形式は、ISO 8601を基に仕様が定義されています。

使える形式

下記の日付(+時刻+タイムゾーン識別子)の組み合わせで表現できます。

  • 日付
    • YYYY-MM-DD
    • YYYY-MM
    • YYYY
  • 時刻
    • THH:mm
    • THH:mm:ss
    • THH:mm:ss.sss
  • タイムゾーン識別子
    • Z+00:00に同じ)
    • -23:59~+23:59

仕様で定められている形式以外にもパース可能なものはありますが、
実行環境によって結果が左右されるため、実際の動作については確認が必要になります。

使用例

console.log(new Date('2021-11-28'));
// -> 2021-11-28T00:00:00.000Z
console.log(new Date('2021-11'));
// -> 2021-11-01T00:00:00.000Z
console.log(new Date('2021'));
// -> 2021-01-01T00:00:00.000Z
console.log(new Date('2021-11-12T12:34:56.789Z'));
// -> 2021-11-12T12:34:56.789Z
console.log(new Date('2021-11T12:34:56.789Z'));
// -> 2021-11-01T12:34:56.789Z
console.log(new Date('2021T12:34:56.789Z'));
// -> 2021-01-01T12:34:56.789Z
console.log(new Date('2021-11-12T12:34:56Z'));
// -> 2021-11-12T12:34:56.000Z
console.log(new Date('2021-11-12T12:34Z'));
// -> 2021-11-12T12:34:00.000Z
console.log(new Date('2021-11-12T12:34:56.789+00:00'));
// -> 2021-11-12T12:34:56.789Z
console.log(new Date('2021-11-12T12:34:56.789-09:00'));
// -> 2021-11-12T21:34:56.789Z
console.log(new Date('2021-11-12T12:34:56.789+09:00'));
// -> 2021-11-12T03:34:56.789Z

参考

1
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
1
1