PDA

View Full Version : Dynamic Countup Script



MydnyteSyn
05-05-2007, 11:27 PM
1) Script Title: Dynamic Countup Script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex6/countingup.htm

3) Describe problem:

Hello DD! I have been using scripts from your website for years! I now come to you with a question concerning the dynamic countup script. I am the web master of a large role-playing community. This script is almost perfect for what the administrators are hoping to use the script for.

Is it possible to make this script display as follows?

It is 1 year 5 months 23 days since (add event here).

Right now it displays the years as days. So basically I'm asking if it's possible to have it display in years. If so, could you please tell me how to achieve that? I have searched the forum but it seems that nothing has been posting in regards to this particularly needed change.

Any assistance at all is appreciated :)

ddadmin
05-06-2007, 07:54 AM
Sure. I've modified the script to support a new "years" value for the "baseunit" parameter. The entire modified script can be found below:


<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&#37;
}

</style>

<script type="text/javascript">

/***********************************************
* Dynamic CountUp script- &#169; 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.
***********************************************/

function dcountup(startingdate, baseunit){
this.currentTime=new Date()
this.startingdate=new Date(startingdate)
this.timesup=false
this.baseunit=baseunit
this.start()
}

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

dcountup.prototype.start=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
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 oneYear=60*60*24*365 //year unit in seconds
var yearfield=Math.floor(timediff/oneYear)
var dayfield=Math.floor((timediff-yearfield*oneYear)/oneDay)
var hourfield=Math.floor((timediff-yearfield*oneYear-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-yearfield*oneYear-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-yearfield*oneYear-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="days"){ //if base unit is days, set "dayfield" to be topmost level
dayfield=yearfield*365+dayfield
yearfield="n/a"
}
else if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=yearfield*365+dayfield*24+hourfield
yearfield=dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=yearfield*365+dayfield*24*60+hourfield*60+minutefield
yearfield=dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
yearfield=dayfield=hourfield=minutefield="n/a"
}
var result={years: yearfield, days: dayfield, hours:hourfield, minutes:minutefield, seconds:secondfield}
this.oncountup(result)
setTimeout(function(){thisobj.start()}, 1000) //update results every second
}

</script>

<body>

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

<script type="text/javascript">

//SYNTAX: myvariable=new dcountup(past_date_and_time_string, "baseunit")
var princewedding=new dcountup("April 9, 2005 13:30:00", "years")

princewedding.oncountup=function(result){
//result is an object containing the current count up date/time, updated every second
//Available properties: result["years"], result["days"], result["hours"], result["minutes"], and result["seconds"]
var mycountainer=document.getElementById("cpcontainer")
mycountainer.innerHTML="Prince Charles and Camilla Parker have been married 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>"
}

</script>

MydnyteSyn
05-06-2007, 03:11 PM
Thank you so much! Sincerely appreciated :)

Friends4U
06-13-2010, 08:16 PM
But what about leap years?