It is currently Fri Jul 28, 2017 12:31 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Improve on "count up Script"?
PostPosted: Fri Jul 25, 2008 9:30 am 
Getting Started
Getting Started

Joined: Thu Jul 17, 2008 3:26 pm
Posts: 51
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>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 12:53 pm 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
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.

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 1:06 pm 
Getting Started
Getting Started

Joined: Thu Jul 17, 2008 3:26 pm
Posts: 51
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 1:10 pm 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
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

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ]  Moderators: Support Team, Moderators

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Hosting | Domains | Servers | Extras | Order | Support | Contacts | FreeHostia © 2011