0

如何从我的网站计数器中删除未使用的零,当它说 0d 0h 23m 4s 时...应该删除“0d”和“0h”...我该怎么做?

下面是我的代码的副本,它可以工作我只是不知道如何阻止零在不需要时显示。示例:计时器读取.. 0d 0h 58m 3s...日期和时间 为零不需要显示,那么如何删除它们?我希望这能让您清楚地了解这里的情况。

var year=2012    //-->Enter the count down target date YEAR
var month=3      //-->Enter the count down target date MONTH
var day=27       //-->Enter the count down target date DAY
var hour=0      //-->Enter the count down target date HOUR (24 hour clock)
var minute=00    //-->Enter the count down target date MINUTE
var tz=-5        //-->Offset for your timezone in hours from UTC (see http://wwp.greenwichmeantime.com/index.htm to find the timezone offset for your location)

//    -->DO NOT CHANGE THE CODE BELOW! This is just a personal comment dont mind it<--
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d,hr,min){
theyear=yr;themonth=m;theday=d;thehour=hr;theminute=min
    var today=new Date()
    var todayy=today.getYear()
    if (todayy < 1000) {todayy+=1900}
    var todaym=today.getMonth()
    var todayd=today.getDate()
    var todayh=today.getHours()
    var todaymin=today.getMinutes()
    var todaysec=today.getSeconds()
    var todaystring1=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
    var todaystring=Date.parse(todaystring1)+(tz*1000*60*60)
    var futurestring1=(montharray[m-1]+" "+d+", "+yr+" "+hr+":"+min);
    var futurestring=Date.parse(futurestring1)-(today.getTimezoneOffset()*(1000*60));
    var dd=futurestring-todaystring
    var dday=Math.floor(dd/(60*60*1000*24)*1)
    var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
    var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
    var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
    if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=0){
        document.getElementById('count').innerHTML=current;
        return;
    }
    else {
        document.getElementById('count').innerHTML=+dday+ "d "+dhour+"h "+dmin+"m "+dsec+"s";
        setTimeout("countdown(theyear,themonth,theday,thehour,theminute)",1000);
    }
}
4

4 回答 4

1

用空字符串替换它是一个想法吗?

document.getElementById('count').innerHTML=current.replace(/0[a-z]\s/gi,'');

在计数的同时:

document.getElementById('count').innerHTML=
                 (+dday > 0 ? +dday + 'd ' : '') +
                 (dhour > 0 ? dhour+'h ' : '') +
                 (dmin > 0 ? dmin+'m ' : '') +
                  dsec+'s';

或(更短)

var val = +dday+ "d "+dhour+"h "+dmin+"m "+dsec+"s";
document.getElementById('count').innerHTML = val.replace(/0[a-z]\s/gi,'');
于 2012-03-26T06:52:11.630 回答
0

试试这个

    else {
count_str = "";
if (dday > 0) {
count_str = count_str + dday + "d ";
}
if (dhour> 0) {
count_str = count_str + dhour + "h ";
}
count_str = count_str + dmin+"m "+dsec+"s"
document.getElementById('count').innerHTML=count_str;
        setTimeout("countdown(theyear,themonth,theday,thehour,theminute)",1000);
    }
于 2012-03-26T06:42:11.010 回答
0

改变这一行:

document.getElementById('count').innerHTML=+dday+ "d "+dhour+"h "+dmin+"m "+dsec+"s";

有条件地显示 dday、dhour 等。例如:

document.getElementById('count').innerHTML= ((dday !=0) ? (dday+ "d ") : "") + ((dhour !=0) ? dhour+"h " : "") +dmin+"m "+dsec+"s";

为了更清楚起见,您可以在多行中构建字符串,如下所示:

var counterStr = "";
if (dday != 0)
  counterStr += dday+ "d ";
if (dhour != 0)
  counterStr += dday + "h ";
counterStr += dmin+"m "+dsec+"s";

document.getElementById('count').innerHTML = counterStr;
于 2012-03-26T07:16:33.330 回答
-1

不要在moment.js已经处理好的事情上大汗淋漓。这个没有依赖关系的小型 javascript 库负责显示日期或时间(到现在为止)。它还为本地化提供了强大的支持。

于 2012-03-26T06:57:22.187 回答