Previous Thread
Next Thread
Print Thread
Rate Thread
Forum Age #109986 02/10/2006 5:30 AM
Joined: Jan 2006
Posts: 11
Raziel Offline OP
Junior Member
OP Offline
Junior Member
Joined: Jan 2006
Posts: 11
Whats wrong with this code?

var startDate = ['08','02','2006'];

// Forum Age
var sta = new Date();
sta.setDate(startDate[0]);
sta.setMonth(startDate[1]-1);
sta.setYear(startDate[2]);
var cur = new Date();
var dif = cur - sta;
var totalMonth = 0;
var totalYear = 0;
var totalDay = Math.floor(dif/(60 * 60 * 24 * 1000)) + 1;
while(totalDay > 365) {
totalDay -= 365;
totalYear++;
}
while(totalDay > 30) {
totalDay -= 30;
totalMonth++;
}
var age = totalDay+' days';
if(totalMonth > 0)
age += ', '+totalMonth+' months';
if(totalYear > 0)
age += ', '+totalYear+' year';
age += '.';

Sponsored Links
Re: Forum Age #109987 02/14/2006 12:00 AM
Joined: May 2001
Posts: 1,042
Likes: 7
Brett Offline
Moderator
Offline
Moderator
Joined: May 2001
Posts: 1,042
Likes: 7
The plus 1 on the totalday variable seems to throwing things off by one day. Fundamentally the month/year counts will be off due to the fact that (leap) years and every other month is not 30 days. Correcting this by taking into account such variations into months will be a hefty amount of javascript.


More or less out of boredom I cleaned up the code taking out the while loops
Code
<script type="text/javascript">
<!--
var startDate = ['08','02','2005'];

// Forum Age
var sta = new Date(startDate[2], startDate[1] - 1, startDate[0]);
var dif = (new Date() - sta) / 86400000; // milliseconds in day
var age, display = new Array(0, 0, 0);

// Find Years
if(dif >= 365) {
display[0] = Math.floor(dif / 365);
dif -= (display[0] * 365); // take days off total
}

// Find Months
if(dif >= 30) {
display[1] = Math.floor(dif / 30);
dif -= (display[1] * 30); // take days off total
}

// Find Days
display[2] = Math.floor(dif);

document.writeln(display[2] + " days, " + display[1] + " months, " + display[0] + " years");
-->
</script>

Re: Forum Age #109988 03/09/2006 7:19 AM
Joined: Jan 2006
Posts: 11
Raziel Offline OP
Junior Member
OP Offline
Junior Member
Joined: Jan 2006
Posts: 11
Thanks Brett smile


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
AllenAyres
AllenAyres
Texas
Posts: 21,079
Joined: March 2000
Forum Statistics
Forums63
Topics37,528
Posts293,719
Members13,834
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online 119
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 20
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,795
Greg Hard 4,625
Top Posters(30 Days)
isaac 4
Gizmo 2
Top Likes Received
isaac 30
Gizmo 8
Brett 7
Morgan 2
Top Likes Received (30 Days)
Gizmo 8
isaac 1
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2020 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Snapshot build 20200118.dev)
Responsive Width:

PHP: 5.4.45 Page Time: 0.037s Queries: 20 (0.011s) Memory: 3.2414 MB (Peak: 3.4474 MB) Data Comp: Zlib Server Time: 2020-01-20 20:33:08 UTC
Valid HTML 5 and Valid CSS