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

后端 未结 21 1456
南笙
南笙 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:04

    I assume you would need the value as string. You could use the code below. It will always return give you the two digit minutes as string.

    var date = new Date(date);
    var min = date.getMinutes();
    
    if (min < 10) {
    min = '0' + min;
    } else {
    min = min + '';
    }
    console.log(min);
    

    Hope this helps.

提交回复
热议问题