A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: how old am I? date calculations

  1. #1
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Hi all,

    I'm trying to use Flash 5 to display how old my web site is - in other words, take the start date (April 8, 1998) and display how old it is ("Floor 42 is x years, x months, x days old". The searches I did on the forums showed me how to count down a date, but trying to apply that logic to counting UP a date has left my math-impaired brain a little stuck.

    I've done what probably amounts to a ridiculous amount of code to get the year and month, but I can't for the life of me figure out how to get the days. If you could take a look at the code below and let me know if there's an easier way to do it, and if you have a solution for the days, I'd be much obliged:

    Code:
    theDate = new Date();
    f42Date = new Date(1998, 3, 8);
    dater = theDate.getDate();
    realYear = theDate.getFullYear();
    f42Year = f42Date.getFullYear();
    fdays = 8-dater;
    // how many years old is Floor 42?
    if (realMonth<3) {
    	// if it is before April
    	f42YearsOld = (realYear-f42year)-1;
    } else if (realMonth=3) {
    	if (dater<8) {
    		// if it is before the 8th
    		f42YearsOld = (realYear-f42year)-1;
    	} else {
    		f42YearsOld = (realYear-f42year);
    	}
    } else {
    	f42YearsOld = (realYear-f42year);
    }
    // how many months old is Floor 42?
    realMonth = theDate.getMonth();
    if (realMonth < 3) {
    	fmonths = realMonth + 9;
    } else if (realMonth >= 4) {
    	fmonths = realMonth - 3;
    } else {
    	if (dater<8) {
    		// if it is before the 8th
    		fmonths = 11;
    	} else {
    		fmonths = 0;
    	}
    }
    // how many days?  this code doesn't work at all!  brain freeze!
    f42TotalDays = 365 * f42YearsOld; // gave up on that
    f42DaysOld = (30 - dater) - 8;  // not sure what I was thinking with this
    // output
    floorCount = "Floor 42 is "+f42YearsOld+" years, "+fmonths+" months, "+f42DaysOld+" days old"
    Ridicule the newly converted Flash 5 user at will ;)

    -- and thank you for any help!!!

    Best,
    Kate

  2. #2
    Moderator
    Join Date
    Aug 2000
    Posts
    1,455
    Hiya!

    There are probably better ways of doing this but the following code should work for you!

    Code:
    launchDate = new Date(Date.UTC(2001, 5, 1));
    
    now = new Date();
    
    timeLeft = int((now-launchDate)/1000);
    
    if (timeLeft != lastTimeLeft) {
        lastTimeLeft = timeLeft;
        timeLeft += now.getTimezoneOffset()*60;
        days = int(timeLeft/(3600*24));
        timeLeft = timeLeft-(days*3600*24);
        hours = int(timeLeft/(3600));
        mins_temp = int(timeLeft/60);
        mins = mins_temp%60;
        secs = timeLeft%60;
        if (Number(days)<10) {
            disp_time = "0" add days;
        } else {
            disp_time = days;
        }
        if (Number(hours)<10) {
            disp_time = disp_time add ":0" add hours;
        } else {
            disp_time = disp_time add ":" add hours;
        }
        if (Number(mins)<10) {
            disp_time = disp_time add ":0" add mins;
        } else {
            disp_time = disp_time add ":" add mins;
        }
        if (Number(secs)<10) {
            disp_time = disp_time add ":0" add secs;
        } else {
            disp_time = disp_time add ":" add secs;
        }
    }
    Obviously change the launchDate at the top but this will output the site age in DD:HH:MM:SS format in a variable called disp_time!

    Enjoy!

    Regards,

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center