17
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?