FreeHostia Forums
https://forum.freehostia.com/

Improve on "count up Script"?
https://forum.freehostia.com/viewtopic.php?f=10&t=9136
Page 1 of 1

Author:  Davestite [ Fri Jul 25, 2008 9:30 am ]
Post subject:  Improve on "count up Script"?

I have following script that tells me how many live days I have been travelling. But I would like it to display in the following format YEARS/MONTHS/DAYS

As in display "Dave has been travelling for 4 years 3 months 12 days"

The current script only tells me how many days I have been travelling

Could anyone help me here please?

<script>

/*
Count up from any date script-
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
*/

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countup(yr,m,d){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy
var paststring=montharray[m-1]+" "+d+", "+yr
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)
difference+=" days"
document.write("It\'s been "+difference+" since Dave has been travelling!")
}
//enter the count up date using the format year/month/day
countup(2005,01,05)
</script>

<p align="center"><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>

Author:  Josh.Waller [ Fri Jul 25, 2008 12:53 pm ]
Post subject: 

Hello,

Try replacing it with this

Code:
<script>

/*
Count up from any date script-
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
Modified for Davestite by Josh.Waller (http://forum.freehostia.com)
*/

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countup(yr,m,d){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy
var paststring=montharray[m-1]+" "+d+", "+yr
var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)

/*Modfication Started by Josh.Waller*/

var year=Math.floor(difference/365)
var xval1=365*year
var xval2=difference - xval1
var month=Math.floor(xval2/30)
var xval3 = 30*month
var day = xval2-xval3


document.write("Dave has been travelling for "+year+ " Years " + month + " Months " + day + " Days")
/*End of Modification Josh.Waller*/

}
//enter the count up date using the format year/month/day
countup(2005,01,05)
</script>


Got the maths in my head in the end. If you want the maths behind the calculation then ok. Its not 100% accurate, because of months that have 31 or 28 days, but i went for 30 days and it works out ok.

Author:  Davestite [ Fri Jul 25, 2008 1:06 pm ]
Post subject: 

Josh!

You are a star! Thanks so much, your maths are spot on!

I have dyslexia and even if I could code just can't do math like that. You're a credit to freehostia.

Big thanks again

Dave

Author:  Josh.Waller [ Fri Jul 25, 2008 1:10 pm ]
Post subject: 

Hi Dave,

Im glad it is, failed A Level Maths though.

I have a form of dyslexia with certian numbers and characters really sucks. I just showed my friend who is doing a Maths degree and she looked well confused :) If you have any other problems like this just post them, i enjoy it.

Josh

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/