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

-1

`<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>`

acfun的怨念 | 初学一级 | 园豆：147

0
``var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);``
``second.innerHTML ="0"+second; //second不是dom元素，没有innerHTML属性``



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

0

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

您需要登录以后才能回答，未注册用户请先注册