首页新闻找找看学习计划

如何让这段代码秒前面加0

-1
悬赏园豆:5 [已解决问题] 解决于 2017-01-08 22:26
<script language="javascript" type="text/javascript">
var interval = 100;
function ShowCountDown(year,month,day,divname)
{
var now = new Date();
var endDate = new Date(year, month-1, day);
var leftTime=endDate.getTime()-now.getTime();
var leftsecond = parseInt(leftTime/1000);
//var day1=parseInt(leftsecond/(24*60*60*6));
var year=Math.floor(leftsecond/(60*60*24*365));
var day1=Math.floor(leftsecond/(60*60*24));
var hour=Math.floor((leftsecond-day1*24*60*60)/3600);
var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
var cc = document.getElementById(divname);

cc.innerHTML = "<h2>"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"+"</h2>";
if(second.length<2)
{
second.innerHTML ="0"+second;
}
}
window.setInterval(function(){ShowCountDown(2017,1,27,'divdown1');}, interval);
</script>
js
问题补充:

是不是调用IF语句有点问题啊

acfun的怨念的主页 acfun的怨念 | 初学一级 | 园豆:147
提问于:2017-01-08 12:22
< >
分享
最佳答案
0
var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
second.innerHTML ="0"+second; //second不是dom元素,没有innerHTML属性



收获园豆:5
JoiWilliam | 小虾三级 |园豆:546 | 2017-01-08 20:57

这点错了,那么要如何改变?

acfun的怨念 | 园豆:147 (初学一级) | 2017-01-08 22:05

@acfun的怨念: 直接赋值:second = "0" + second;


JoiWilliam | 园豆:546 (小虾三级) | 2017-01-08 22:09

@JoiWilliam: 哥们不行啊

acfun的怨念 | 园豆:147 (初学一级) | 2017-01-08 22:11

@acfun的怨念:

if(second.length<2)
{
        second = "0" + second;
}
// 注意赋值逻辑
cc.innerHTML = "<h2>"+day1+"天"+hour+"小时"+minute+"分"+second+"秒"+"</h2>";



JoiWilliam | 园豆:546 (小虾三级) | 2017-01-08 22:16

@JoiWilliam: 还是不行

acfun的怨念 | 园豆:147 (初学一级) | 2017-01-08 22:18

@acfun的怨念: if(second.length<2)改成if(second<10)


JoiWilliam | 园豆:546 (小虾三级) | 2017-01-08 22:21

@JoiWilliam: 多谢

acfun的怨念 | 园豆:147 (初学一级) | 2017-01-08 22:25
其他回答(1)
0

写一个补充方法.所有的输出都调用这个方法.

方法就是输入一个字符串.再输入一个最小长度.当长度不够是在恰面加0补满.

PS.net的string里是自带的.js不知道

吴瑞祥 | 园豆:28729 (高人七级) | 2017-01-08 12:51

大神啊,我主要的确不太会啊,能给点提示不?

支持(0) 反对(1) acfun的怨念 | 园豆:147 (初学一级) | 2017-01-08 13:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册