Results 1 to 3 of 3

Thread: How do I get rid of years?

  1. #1
    Join Date
    Jan 2009
    Location
    NH
    Posts
    673
    Thanks
    97
    Thanked 26 Times in 26 Posts

    Question How do I get rid of years?

    On this page I have a countdown script. I don't want the year to be shown. I tried taking out all the code that relates to y or year but then the code didn't work.

    Code:
    <script type="text/javascript">
    //###################################################################
    // Author: ricocheting.com
    // Version: v3.0
    // Date: 2014-09-05
    // Description: displays the amount of time until the "dateFuture" entered below.
    
    var CDown = function() {
    	this.state=0;// if initialized
    	this.counts=[];// array holding countdown date objects and id to print to {d:new Date(2013,11,18,18,54,36), id:"countbox1"}
    	this.interval=null;// setInterval object
    }
    
    CDown.prototype = {
    	init: function(){
    		this.state=1;
    		var self=this;
    		this.interval=window.setInterval(function(){self.tick();}, 1000);
    	},
    	add: function(date,id){
    		this.counts.push({d:date,id:id});
    		this.tick();
    		if(this.state==0) this.init();
    	},
    	expire: function(idxs){
    		for(var x in idxs) {
    			this.display(this.counts[idxs[x]], "Now!");
    			this.counts.splice(idxs[x], 1);
    		}
    	},
    	format: function(r){
    		var out="";
    		out += r.y +" "+((r.y==1)?"year":"years")+", ";
    		if(r.w != 0){out += r.w +" "+((r.w==1)?"week":"weeks")+", ";}
    		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);
    	},
    	math: function(work){
    		var	y=w=d=h=m=s=ms=0;
    
    		ms=(""+((work%1000)+1000)).substr(1,3);
    		work=Math.floor(work/1000);//kill the "milliseconds" so just secs
    
    		y=Math.floor(work/31536000);//years (no leapyear support)
    		work=work%31536000;
    
    		w=Math.floor(work/604800);//weeks
    		work=work%604800;
    
    		d=Math.floor(work/86400);//days
    		work=work%86400;
    
    		h=Math.floor(work/3600);//hours
    		work=work%3600;
    
    		m=Math.floor(work/60);//minutes
    		work=work%60;
    
    		s=Math.floor(work);//seconds
    
    		return {y:y,w:w,d:d,h:h,m:m,s:s,ms:ms};
    	},
    	tick: function(){
    		var now=(new Date()).getTime(),
    			expired=[],cnt=0,amount=0;
    
    		if(this.counts)
    		for(var idx=0,n=this.counts.length; idx<n; ++idx){
    			cnt=this.counts[idx];
    			amount=cnt.d.getTime()-now;//calc milliseconds between dates
    
    			// if time is already past
    			if(amount<0){
    				expired.push(idx);
    			}
    			// date is still good
    			else{
    				this.display(cnt, this.format(this.math(amount)));
    			}
    		}
    
    		// deal with any expired
    		if(expired.length>0) this.expire(expired);
    
    		// if no active counts, stop updating
    		if(this.counts.length==0) window.clearTimeout(this.interval);
    		
    	},
    	display: function(cnt,msg){
    		document.getElementById(cnt.id).innerHTML="Time to the Festival: " +msg;
    	}
    };
    
    window.onload=function(){
    	var cdown = new CDown();
    
    	cdown.add(new Date(2015,6,17,17,00,00), "countbox1");
    };
    </script>
    <div id="countbox1" style="font:16pt Arial; color:#FF0000;"></div>

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    The quickest way is to avoid outputting "Years" if r.y is zero. Edit the line shown in red below.


    Code:
    	format: function(r){
    		var out="";
    		if(r.y != 0){out += r.y +" "+((r.y==1)?"year":"years")+", ";}
    		if(r.w != 0){out += r.w +" "+((r.w==1)?"week":"weeks")+", ";}
    		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);
    	},

  3. The Following User Says Thank You to styxlawyer For This Useful Post:

    mlegg (05-06-2015)

  4. #3
    Join Date
    Jan 2009
    Location
    NH
    Posts
    673
    Thanks
    97
    Thanked 26 Times in 26 Posts

    Default

    Thank you

Similar Threads

  1. 18+ years register control
    By auriaks in forum PHP
    Replies: 3
    Last Post: 06-04-2011, 03:59 PM
  2. Resolved help please i a finally found this site again after 8 years
    By ticoman in forum JavaScript
    Replies: 2
    Last Post: 09-16-2010, 02:35 PM
  3. adding years to countup script
    By durangod in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 11-22-2009, 05:34 PM
  4. BC Years Without A Minus Sign
    By TimeTracker in forum JavaScript
    Replies: 10
    Last Post: 07-24-2008, 11:40 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
  •