Skip to content

Instantly share code, notes, and snippets.

@LeeDDHH
Created May 18, 2022 23:51
Show Gist options
  • Save LeeDDHH/ce5f53e15ccafe4cb39a2575b1fbc8bd to your computer and use it in GitHub Desktop.
Save LeeDDHH/ce5f53e15ccafe4cb39a2575b1fbc8bd to your computer and use it in GitHub Desktop.
javascriptでDateオブジェクトを扱うためのあれこれ

日本時刻を基準にISO8601形式の日付を「hh:mm」形式で整形する

const ISO8601 = '2022-05-19T17:00:00+09:00';
const hourMinutes = new Date(ISO8601)
    // ロケールを日本に指定
    // 「言語コード-国コード」形式の引数とtimeZoneの指定
    // → 2022/5/19 17:00:00
    .toLocaleString('ja-JP', { timeZone: 'Asia/Tokyo' })
    // スペースで日付と時間を区切る
    // → ['2022/5/19', '17:00:00']
    .split(' ')[1]
    // 「時間:分:秒」形式の文字列から後ろの「:秒」を削除する
    // → 17:00
    .slice(0,-3);
console.log(hourMinutes); // 17:00

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment