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

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

    I usually use this piece of code :

    var start = new Date(timestamp),
        startMinutes = start.getMinutes() < 10 ? '0' + start.getMinutes() : start.getMinutes();
    

    It is quite similar to the @ogur accepted answer but does not concatenate an empty string in the case that 0 is not needed. Not sure it is better. Just an other way to do it !

提交回复
热议问题