Javascript counter add more
3 posters
Page 1 of 1
Javascript counter add more
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
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 Sun 2 Aug - 12:38; edited 1 time in total
Re: Javascript counter add more
Hey @Mati,
I just added codes that will show months & years on your counter. This should work. Try this:
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- Active Poster
- Posts : 1407
Reputation : 236
Language : English, Filipino
Location : Philippines
Re: Javascript counter add more
Mati wrote:Hello, Russel
Good to see you again and thanks for the help
No problem bro. Glad i could help.
Russel- Active Poster
- Posts : 1407
Reputation : 236
Language : English, Filipino
Location : Philippines
Similar topics
» Modify Minimum Word Counter (JavaScript)
» Profile View Counter & Website View Counter
» How do i add a web counter
» Help with word counter?
» A word counter
» Profile View Counter & Website View Counter
» How do i add a web counter
» Help with word counter?
» A word counter
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum