Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ Automatically update age from birth year

Posted by: jernatety Sep 20 2019, 12:05 PM

Hello,

On my sports website I have created "Player" profile pages which include a players birth year and age.

The entire pages is written in HTML. I do not know anything other than HTML and I'm not an expert, I'm novice at best. I'm now up to 30 player profile pages and there's no way I'm going to remember to manually update each of their age's every time it's their birthday.

Is there any way with HTML or javascript that I can have the ages automatically update each time their birthday comes up?

Thank you for any help in advance. I can provide a link to a page to review upon request.

Posted by: CharlesEF Sep 20 2019, 08:59 PM

Try this:

CODE
function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

Or this, faster:
CODE
function calcAge(dateString) {
  var birthday = new Date(dateString);
  var ageDifMs = Date.now() - birthday.getTime();
  var ageDate = new Date(ageDifMs); // miliseconds from epoch
  return Math.abs(ageDate.getFullYear() - 1970);
}

Posted by: jernatety Sep 21 2019, 04:51 AM

Thank you for the reply!

I tried adding this but after I save and view the live page all I see is the code.

Posted by: Christian J Sep 21 2019, 05:18 AM

You need to put the script in a SCRIPT element:

CODE
<script type="text/javascript">
function calcAge(dateString) {
  var birthday = new Date(dateString);
  var ageDifMs = Date.now() - birthday.getTime();
  var ageDate = new Date(ageDifMs); // miliseconds from epoch
  return Math.abs(ageDate.getFullYear() - 1970);
}
</script>


You must also call the functions somehow, and retrieve the birth year from the player profile. Exactly how to do this depends on how your current page is written, so a link or sample code would be indeed useful.

Posted by: jernatety Sep 21 2019, 05:24 AM

QUOTE(Christian J @ Sep 21 2019, 06:18 AM) *

You need to put the script in a SCRIPT element:

CODE
<script type="text/javascript">
function calcAge(dateString) {
  var birthday = new Date(dateString);
  var ageDifMs = Date.now() - birthday.getTime();
  var ageDate = new Date(ageDifMs); // miliseconds from epoch
  return Math.abs(ageDate.getFullYear() - 1970);
}
</script>


You must also call the functions somehow, and retrieve the birth year from the player profile. Exactly how to do this depends on how your current page is written, so a link or sample code would be indeed useful.


My site is entirely built with a free community script called SMF. I am using an add on module called Tiny Portal. Tiny Portal has a feature which allows you to make pages called "Articles" in HTML. This is one of the pages. You'll see in the middle of the page is where the players birth date and age are. For this example, this kids full birth date isn't available. Almost all of the other pages the players full birth date is available.

CODE
https://www.youthhockeyinfo.com/index.php?page=28

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)