Javascript Date: Ensure getMinutes(), getHours(), getSeconds() puts 0 in front if necessary

前端 未结 3 824
走了就别回头了
走了就别回头了 2020-12-08 08:05

Looking for a creative way to be sure values that come from the getHours, getMinutes, and getSeconds() method for the javascript Date object return \"06\" inste

3条回答
  •  伪装坚强ぢ
    2020-12-08 08:13

    function pad2(number) {
    
         return (number < 10 ? '0' : '') + number
    
    }
    
    document.write(pad2(2) + '
    '); document.write(pad2(10) + '
    ');

    OUTPUT:

    02

    10

提交回复
热议问题