Leading zeros in minutes

后端 未结 5 1923
眼角桃花
眼角桃花 2020-12-31 17:20

I created a clock to be placed in the header of my website. The time is not displaying a zero for minutes < 10. For example if the time is 10:50, it will only show 10:5 ,

5条回答
  •  Happy的楠姐
    2020-12-31 18:08

    And what is your issue?

    var minutes = (current.getMinutes() < 10? '0' : '') + current.getMinutes();
    

    Since you'll have the same problem with hours, wrap it in a small utility function:

    function pad(var value) {
        if(value < 10) {
            return '0' + value;
        } else {
            return value;
        }
    }
    

    And later simply:

    pad(current.getHours()) + ":" + pad(current.getMinutes())
    

提交回复
热议问题