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

后端 未结 21 1523
南笙
南笙 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 00:57

    Numbers don't have a length, but you can easily convert the number to a string, check the length and then prepend the 0 if it's necessary:

    var strMonth = '' + date.getMinutes();
    if (strMonth.length == 1) {
      strMonth = '0' + strMonth;
    }
    

提交回复
热议问题