Looking for automatic age updater?

Looking for automatic age updater? I googled for a bit but couldnt find it. I always forget to update my age on my site so Im looking for a way to make this automatic like a auto current year one I have for my copyright. Thanks!

So my sentence would read… Hello name name is yada - yada my age is 35 (js goes here in place of 35).

Hi Eric can’t you just use nearly the same script you’re using for the copyright. Just set the d.o.b. and use a datediff between now() and d.o.b.

I’m not that familiar with P.H.P but here is how I would do it in Coldfusion


<cfset dob = dateFormat( '1961-03-18', 'yyyy-mm-dd' )/>
<cfset nowDate = dateFormat( now(), 'yyyy-mm-dd' ) />

<cfset age = dateDiff( 'yyyy', dob, nowDate  ) />
<cfdump var="#age#" />

Wow! While sitting in my truck I have my music streaming from my iPhone playing through my truck speakers, and all awhile multitasking and reading/replying to this post, and launch dropbox to see and copy my copyright code into this post - tecknowledgey is amazing! I am now 100% on my phone. Aside from website edits I have completely abandoned my desktop. I fax, email, scan, files, banking, etc, etc all from my phone.

OK, here is my copyright code. I don’t think I can do that. Or can I?

<script>document.write("2002 - "+ new Date().getFullYear());</script>

Eric here is A Link to date_diff using javascrip.

Enjoy your music :slight_smile:

Thanks but unless mistaken I don’t believe that will work. I don’t need a date updated I need a age (eg 35) updated on a specific date each year.

The date documentation page should give you all the tools that you need.

For example:


var now = new Date();
var birth = new Date('1961-03-18');
var difference = now - birth;
// Dates are set from 1970, so remove that to get the year 
var age = new Date(difference).getFullYear() - 1970
// age is 49

It should work though. If you set your birth day and you use the date_diff (YEARS) function to determine the difference between today and the your birth day it should return the number of years.

Cool. Ok, I’m going to show my ignorance here. So the var stuff goes in the head (or above the age/html) right? And then I call it in the HTML how? Thanks again!

No, because from the head you cannot interact with the body of the page.

You place the script at the end of the body, just before the </body> tag.


yada yada my age is <span id="age">unknown</span>
...
<script type="text/javascript">
...
</script>
</body>
</html>


var birth = new Date('1961-03-18');
var now = new Date();
var difference = now - birth;
// Dates are set from 1970, so remove that to get the year 
var age = new Date(difference).getFullYear() - 1970

var span = document.getElementById('age');
span.innerHTML = age;

Awesome - thank you Paul and donboe! :slight_smile: I should be able to handle it from there. Then I’ll hide the physical age with js so it shows with js on or off. I’ll work on that tomorrow morning. Will post back if I hit trouble.

Hmmmm… It works! But I don’t get the math its doing? For instance, if I put in my wifes date of birth and her year born (1975) it spits out that she is 40 years old. But she is 35 years old. So I have to use a false date in order to spit out the correct age. Am I missing something? Thanks!

<script type=“text/javascript”>
var birth = new Date(‘1975-06-05’);
var now = new Date();
var difference = now - birth;
// Dates are set from 1970, so remove that to get the year
var age = new Date(difference).getFullYear() - 1975

var span = document.getElementById(‘age’);
span.innerHTML = age;
</script>

Like pmw57 said

// Dates are set from 1970, so remove that to get the year

I did. Using the js I just posted above it spits out that she is 40. It should be saying she is 35. She was born in 1975. However, in order to spit out 35 I need to say she was born on 1970 in the script. This is why I say I don’t get the math.

The script must ALWAYS subtract 1970 to arrive at the correct age. Your wifes birth year is placed in only the one location, and that is the at the top of the script.

Ahhh… OK that works! Trying to learn a little here. Why 1970? Is that the magic number you just used to make the script work or does it have some deeper js meaning? And I assume the script will automatically change her birthdate to 36 on her birthday correct? Thanks! :slight_smile:

Oops. Just noticed! I have one more request please. I need to show both of our ages. Cant use same id twice on same page so whats the best/easiest way to do that? I prob could just use the script twice buts thats sloppy yeah? Can I coma separate the id’s like… var span = document.getElementById(‘age,age2’)? But of course our birthdates are different so maybe two scripts may be the easiest route there? Or not?

Because 1970 is when most computers start counting dates from.
See: http://en.wikipedia.org/wiki/Unix_time

Turn the script in to a function that accepts a birth date and target location as its parameters.

Easier said than done (for me at least). I’ll read up on doing that and use two scripts as my fallback. Either way I’m happy thank you. One less thing to remember to do next year.

function showAge(birthDate, id) {

}