The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Javascript counter add more

3 posters

Go down

Solved Javascript counter add more

Post by Mati July 31st 2015, 6:03 pm

Hello,

I have this code which counts days, hours, minutes and seconds but I also want to add year and month in to it.

Ex:

3 years 5 months 5 days 5 hours 5 mins 5 secs

This how it looks now -> http://idesign.forumotion.net/forum

Code:
<script type="text/javascript">
/**********************************************************************************************
* Days-Hours-Minutes-Seconds Counter script by Praveen Lobo
* (http://PraveenLobo.com/techblog/javascript-counter-count-days-hours-minutes-seconds/)
* This notice MUST stay intact(in both JS file and SCRIPT tag) for legal use.
* http://praveenlobo.com/blog/disclaimer/
**********************************************************************************************/
function DaysHMSCounter(initDate, id){
    this.counterDate = new Date(initDate);
    this.container = document.getElementById(id);
    this.update();
}
 
DaysHMSCounter.prototype.calculateUnit=function(secDiff, unitSeconds){
    var tmp = Math.abs((tmp = secDiff/unitSeconds)) < 1? 0 : tmp;
    return Math.abs(tmp < 0 ? Math.ceil(tmp) : Math.floor(tmp));
}
 
DaysHMSCounter.prototype.calculate=function(){
    var secDiff = Math.abs(Math.round(((new Date()) - this.counterDate)/1000));
    this.days = this.calculateUnit(secDiff,86400);
    this.hours = this.calculateUnit((secDiff-(this.days*86400)),3600);
    this.mins = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)),60);
    this.secs = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)-(this.mins*60)),1);
}
 
DaysHMSCounter.prototype.update=function(){
    this.calculate();
    this.container.innerHTML =
        " <strong>" + this.days + "</strong> " + (this.days == 1? "day" : "days") +
        " <strong>" + this.hours + "</strong> " + (this.hours == 1? "hour" : "hours") +
        " <strong>" + this.mins + "</strong> " + (this.mins == 1? "min" : "mins") +
        " <strong>" + this.secs + "</strong> " + (this.secs == 1? "sec" : "secs");
    var self = this;
    setTimeout(function(){self.update();}, (1000));
}
 
window.onload=function(){ new DaysHMSCounter('July 14, 2015 12:00:00', 'counter'); }
 
</script>
<div id="counter">
                 Contents of this DIV will be replaced by the timer             
</div>


Last edited by Mati on August 2nd 2015, 6:38 pm; edited 1 time in total
Mati
Mati
Hyperactive

Posts : 2020
Reputation : 330
Language : HTML, CSS & JavaScript
Location : Forum Services

https://forumservice.forumotion.com/

Back to top Go down

Solved Re: Javascript counter add more

Post by Mati August 2nd 2015, 2:03 pm

Bump...
Mati
Mati
Hyperactive

Posts : 2020
Reputation : 330
Language : HTML, CSS & JavaScript
Location : Forum Services

https://forumservice.forumotion.com/

Back to top Go down

Solved Re: Javascript counter add more

Post by Russel August 2nd 2015, 4:07 pm

Hey @Mati,

I just added codes that will show months & years on your counter. This should work. Try this:  
Code:
<script type="text/javascript">
/**********************************************************************************************
* Days-Hours-Minutes-Seconds Counter script by Praveen Lobo
* (http://PraveenLobo.com/techblog/javascript-counter-count-days-hours-minutes-seconds/)
* This notice MUST stay intact(in both JS file and SCRIPT tag) for legal use.
* http://praveenlobo.com/blog/disclaimer/
**********************************************************************************************/
function DaysHMSCounter(initDate, id){
    this.counterDate = new Date(initDate);
    this.container = document.getElementById(id);
    this.update();
}
 
DaysHMSCounter.prototype.calculateUnit=function(secDiff, unitSeconds){
    var tmp = Math.abs((tmp = secDiff/unitSeconds)) < 1? 0 : tmp;
    return Math.abs(tmp < 0 ? Math.ceil(tmp) : Math.floor(tmp));
}
 
DaysHMSCounter.prototype.calculate=function(){
    var secDiff = Math.abs(Math.round(((new Date()) - this.counterDate)/1000));
    this.years = this.calculateUnit(secDiff,31536000);
    this.months = this.calculateUnit(secDiff,2628000);
    this.days = this.calculateUnit(secDiff,86400);
    this.hours = this.calculateUnit((secDiff-(this.days*86400)),3600);
    this.mins = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)),60);
    this.secs = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)-(this.mins*60)),1);
}
 
DaysHMSCounter.prototype.update=function(){
    this.calculate();
    this.container.innerHTML =
         " <strong>" + this.years + "</strong> " + (this.years == 1? "year" : "years") +
         " <strong>" + this.months + "</strong> " + (this.months == 1? "month" : "months") +
        " <strong>" + this.days + "</strong> " + (this.days == 1? "day" : "days") +
        " <strong>" + this.hours + "</strong> " + (this.hours == 1? "hour" : "hours") +
        " <strong>" + this.mins + "</strong> " + (this.mins == 1? "min" : "mins") +
        " <strong>" + this.secs + "</strong> " + (this.secs == 1? "sec" : "secs");
    var self = this;
    setTimeout(function(){self.update();}, (1000));
}
 
window.onload=function(){ new DaysHMSCounter('July 14, 2015 12:00:00', 'counter'); }
 
</script>
<div id="counter">
                          Contents of this DIV will be replaced by the timer                    
</div>
Russel
Russel
Active Poster

Male Posts : 1407
Reputation : 236
Language : English, Filipino
Location : Philippines

Back to top Go down

Solved Re: Javascript counter add more

Post by Mati August 2nd 2015, 6:38 pm

Hello, Russel

Good to see you again Smile and thanks for the help
Mati
Mati
Hyperactive

Posts : 2020
Reputation : 330
Language : HTML, CSS & JavaScript
Location : Forum Services

https://forumservice.forumotion.com/

Back to top Go down

Solved Re: Javascript counter add more

Post by Russel August 2nd 2015, 6:47 pm

Mati wrote:Hello, Russel

Good to see you again Smile and thanks for the help  

No problem bro. Glad i could help. Very good
Russel
Russel
Active Poster

Male Posts : 1407
Reputation : 236
Language : English, Filipino
Location : Philippines

Back to top Go down

Solved Re: Javascript counter add more

Post by Ape August 3rd 2015, 2:41 pm

Topic solved and archived


Javascript counter add more  Left1212Javascript counter add more  Center11Javascript counter add more  Right112
Javascript counter add more  Ape_b110
Javascript counter add more  Ape1010
Ape
Ape
Administrator
Administrator

Male Posts : 19084
Reputation : 1988
Language : fluent in dork / mumbojumbo & English haha

http://chatworld.forumotion.co.uk/

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum