getMinutes() 0-9 - How to display two digit numbers?

后端 未结 21 1526
南笙
南笙 2020-12-01 00:42
var date = \"2012-01-18T16:03\";
var date = new Date(date);

console.log(date.getMinutes());
console.log(date.getMinutes().length)

This returns 3.

21条回答
  •  天涯浪人
    2020-12-01 01:11

    Another short way is to fill the minutes with a leading zero using:

    String(date.getMinutes()).padStart(2, "0");
    

    Meaning: Make the string two chars long, if a char is missing then set 0 at this position.

    See docs at str.padStart(targetLength, padString)

提交回复
热议问题