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

后端 未结 21 1393
南笙
南笙 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

    If you're using AngularJS in your project, just inject $filter and use it like here:

    $filter('date')(value, 'HH:mm')
    

    You can also format the output in the template, more on filters here.

    0 讨论(0)
  • 2020-12-01 01:17
    var date = new Date("2012-01-18T16:03");
    
    console.log( (date.getMinutes()<10?'0':'') + date.getMinutes() );
    
    0 讨论(0)
  • 2020-12-01 01:17

    Another option:

    var dateTime = new Date();
    var minutesTwoDigitsWithLeadingZero = ("0" + dateTime.getMinutes()).substr(-2);
    
    0 讨论(0)
提交回复
热议问题