LoginSignup
15
8

More than 3 years have passed since last update.

JavaScriptで時刻の二桁目をゼロ埋めする

Last updated at Posted at 2020-03-05

取得した時刻をStringへ変換してpadStart

1時2分を「1:2」ではなく「01:02」みたいに表記したかった。
忘れないようにメモ。

console
const d = new Date('2020-03-05T01:02:03');
const hourStr   = d.getHours().toString().padStart(2, '0');
const minuteStr = d.getMinutes().toString().padStart(2, '0');
console.log(hourStr + ":" + minuteStr); // 01:02

参考資料

String.prototype.padStart() - JavaScript | MDN

15
8
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
15
8