Results 1 to 4 of 4

Thread: Time countdown with plural/singular increments

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default Time countdown with plural/singular increments

    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

    Code:
    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

    Code:
    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);
    	},
    Last edited by FrickenTrevor; 12-04-2014 at 10:56 PM.
    An inline div is a freak of the web and should be beaten until it becomes a span

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    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);
    }
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    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:
    Click image for larger version. 

Name:	christmas-countdown-og.jpg 
Views:	151 
Size:	77.3 KB 
ID:	5558

    My plan is to write it out in JavaScript and then style it using CSS.
    Last edited by FrickenTrevor; 12-04-2014 at 10:55 PM.
    An inline div is a freak of the web and should be beaten until it becomes a span

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Similar Threads

  1. Resolved Countdown for DATE and TIME
    By Jay Dog in forum Flash
    Replies: 1
    Last Post: 04-30-2014, 12:48 PM
  2. Countdown to input time.
    By kirtangl in forum JavaScript
    Replies: 6
    Last Post: 08-18-2008, 10:37 PM
  3. Replies: 2
    Last Post: 08-11-2008, 05:37 PM
  4. Display Countdown Time Remaining
    By penster1 in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-05-2008, 12:29 AM
  5. Universal Countdown after the event time
    By spoonervt in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 02-29-2008, 12:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •