Leading zeros in minutes

后端 未结 5 1918
眼角桃花
眼角桃花 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条回答
  •  -上瘾入骨i
    2020-12-31 18:00

    Since you're likely to run into presentational issues in the future along the same lines, I'd recommend picking a favorite string formatting function for Javascript.

    Some examples:

    • http://www.masterdata.se/r/string_format_for_javascript/
    • http://www.diveintojavascript.com/projects/javascript-sprintf

    Then you can do something like "{0:00}:{1:00}".format(current.getHours(), current.getMinutes()) or even better,

    var d = new Date();
    var s = d.format("hh:mm:ss tt");
    // Result: "02:28:06 PM"
    

提交回复
热议问题