Javascript 如何格式化前导零的数字?
我的时间正在通过以下功能实时更新:Javascript 如何格式化前导零的数字?,javascript,jquery,Javascript,Jquery,我的时间正在通过以下功能实时更新: var-nIntervId; 函数updateTime(){ nIntervId=设置间隔(闪速时间,1000*2); } 函数flashTime(){ var now=新日期(); var h=now.getHours(); var m=now.getMinutes(); var s=now.getSeconds(); 变量时间=h+':'+m; $('.time').html(time); } $(函数(){ updateTime(); }); 问题
var-nIntervId;
函数updateTime(){
nIntervId=设置间隔(闪速时间,1000*2);
}
函数flashTime(){
var now=新日期();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds();
变量时间=h+':'+m;
$('.time').html(time);
}
$(函数(){
updateTime();
});代码>
问题是,如果是11:09,它将显示11:9
您可以添加如下条件:
var m = now.getMinutes() < 10? '0'+now.getMinutes(): now.getMinutes();
var m=now.getMinutes()0'+now.getMinutes():now.getMinutes();
如果分钟数小于10,则将添加0
。问题标题应为“如何使用前导零设置数字格式”。@PeterB是,正确。我会改变的