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

后端 未结 21 1448
南笙
南笙 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条回答
  •  -上瘾入骨i
    2020-12-01 00:53

    I would like to provide a more neat solution to the problem if I may.The accepted answer is very good. But I would have done it like this.

    Date.prototype.getFullMinutes = function () {
       if (this.getMinutes() < 10) {
           return '0' + this.getMinutes();
       }
       return this.getMinutes();
    };
    

    Now if you want to use this.

    console.log(date.getFullMinutes());
    

提交回复
热议问题