View Full Version : Updated Upcounter Code

11-22-2009, 07:09 PM
1) CODE TITLE: Upcounter

2) AUTHOR NAME/NOTES: Dynamic Drive orginal code and updated by Durangodaveswebdesign.com on 11-22-09

3) DESCRIPTION: This was the original code provided by Dynamic Drive at the url http://www.dynamicdrive.com/dynamicindex6/countingup.htm and has been updated by durangodaveswebdesign.com to include the addition of the year field.


or, ATTACHED BELOW (see #3 in guidelines below):

<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<title> upcounter </title>

<style style="text/css">

.dcountstyle{ /*Example CSS to style count up output*/
font: bold 16px Arial;
padding: 3px;

.dcountstyle sup{ /*Example CSS to style count up output*/
font-size: 90%


<script type="text/javascript">

* Dynamic CountUp script- Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
* Updates: Orginal code from Dynamic Drive
* Updated adding the year function by Durangodaveswebdesign.com
* on 11-22-09

function dcountup(startingdate, baseunit){
this.currentTime=new Date()
this.startingdate=new Date(startingdate)

dcountup.prototype.oncountup=function(){} //default action for "oncountup"

var thisobj=this
var timediff=(this.currentTime-this.startingdate)/1000 //difference btw target date and current date, in seconds
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff

var divyears = (dayfield) / 365 <!-- take number of days and div by 365 to get nbr of years (unrounded) -->
var totyears = Math.floor (divyears) <!-- round the nbr of years to whole number -->

<!-- var leftoverdays = dayfield - (divyears * 365 ) not used - uses (unrounded total years), duplicate of next
line code just dif way to result -->

var years2days = totyears * 365 <!-- multiply (rounded) number of years x 365 then to get total
number of days in the total years. -->
dayfield = dayfield - years2days <!-- then subtract that number of days in total years from the total number in
the original dayfield to reset the dayfield to show remainder of days after
years have been removed -->

var result={years: totyears, days: dayfield, hours:hourfield, minutes:minutefield, seconds:secondfield}
setTimeout(function(){thisobj.start()}, 1000) //update results every second




<div id="cpcontainer" align="center">&nbsp;</div>

<script type="text/javascript">

//SYNTAX: myvariable=new dcountup(past_date_and_time_string, "baseunit")
var princewedding=new dcountup("November 22, 2008 08:00:00", "days")

//result is an object containing the current count up date/time, updated every second
//Available properties: result["days"], result["hours"], result["minutes"], and result["seconds"]
var mycountainer=document.getElementById("cpcontainer")
mycountainer.innerHTML="This Site Has Been Active For: <br /><span class='dcountstyle'>"+result['years']+"<sup> years</sup> "+result['days']+" <sup>days</sup> "+result['hours']+" <sup>hours</sup> "+result['minutes']+" <sup>minutes</sup> "+result['seconds']+" <sup>seconds</sup></span>"



11-22-2009, 08:25 PM
i just added some comments in the file for my updates and also notated a var that is not used but maybe used depending if you want to use the rounded or unrounded years for your mathmatics.

06-13-2010, 08:13 PM
I think you forget about leap years... other than that the function is very nice!