PDA

View Full Version : Time countdown with plural/singular increments



FrickenTrevor
12-04-2014, 02:33 PM
There is this script which works. However the minutes, secondes, days are not plural or singular. Such as "1 seconded" instead of "1 secondes". This is the first part



function cdtd() {
var xmas = new Date("December 25, 2012 00:01:00");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
document.write("Christmas is here!");
// Run any code needed for countdown completion here
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
var timer = setTimeout('cdtd()',1000);
}


The other script takes the days, minutes, seconds and makes it plural/singular. However I cant figure out how to call it in the code above



format: function(r){
var out="";
if(r.d != 0){out += r.d +" "+((r.d==1)?"day":"days")+", ";}
if(r.h != 0){out += r.h +" "+((r.h==1)?"hour":"hours")+", ";}
out += r.m +" "+((r.m==1)?"min":"mins")+", ";
out += r.s +" "+((r.s==1)?"sec":"secs")+", ";

return out.substr(0,out.length-2);
},

vwphillips
12-04-2014, 04:03 PM
function cdtd() {
var xmas = new Date("December 25, 2012 00:01:00");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
document.write("Christmas is here!");
// Run any code needed for countdown completion here
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById("daysBox").innerHTML = days+'day'+(days!=1?'s':'');
document.getElementById("hoursBox").innerHTML = hours+'hour'+(hours!=1?'s':'');
document.getElementById("minsBox").innerHTML = minutes+'minute'+(minutes!=1?'s':'');
document.getElementById("secsBox").innerHTML = seconds+'second'+(seconds!=1?'s':'');
var timer = setTimeout('cdtd()',1000);
}

FrickenTrevor
12-04-2014, 07:21 PM
Thanks :)

EDIT: I ran the code and noticed that the "seconds/minutes..." are added in with the time. I also forgot to post that I am trying to style the text to look something like this:
5558

My plan is to write it out in JavaScript and then style it using CSS.

vwphillips
12-05-2014, 10:44 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>

</head>

<body>
<table>
<tr>
<td id="daysBox" ></td>
<td id="hoursBox" ></td>
<td id="minsBox" ></td>
<td id="secsBox" ></td>
</tr>
<tr>
<td id="daysBox2" ></td>
<td id="hoursBox2" ></td>
<td id="minsBox2" ></td>
<td id="secsBox2" ></td>
</tr>
</table>
<script type="text/javascript">
/*<![CDATA[*/
function cdtd() {
var xmas = new Date("December 25, 2014 00:01:00");
var now = new Date();
var timeDiff = xmas.getTime() - now.getTime();
if (timeDiff <= 0) {
clearTimeout(timer);
// document.write("Christmas is here!");
// Run any code needed for countdown completion here
}
var seconds = Math.floor(timeDiff / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
hours %= 24;
minutes %= 60;
seconds %= 60;
document.getElementById("daysBox").innerHTML = days;
document.getElementById("hoursBox").innerHTML = hours;
document.getElementById("minsBox").innerHTML = minutes;
document.getElementById("secsBox").innerHTML = seconds;
document.getElementById("daysBox2").innerHTML = 'day'+(days!=1?'s':'');
document.getElementById("hoursBox2").innerHTML = 'hour'+(hours!=1?'s':'');
document.getElementById("minsBox2").innerHTML = 'minute'+(minutes!=1?'s':'');
document.getElementById("secsBox2").innerHTML = 'second'+(seconds!=1?'s':'');
var timer = setTimeout('cdtd()',1000);
}

cdtd()
/*]]>*/
</script>
</body>

</html>