The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Automatically update age from birth year
jernatety
post Sep 20 2019, 12:05 PM
Post #1


Newbie
*

Group: Members
Posts: 12
Joined: 12-April 19
Member No.: 26,876



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 20 2019, 08:59 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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);
}


This post has been edited by CharlesEF: Sep 20 2019, 09:04 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jernatety
post Sep 21 2019, 04:51 AM
Post #3


Newbie
*

Group: Members
Posts: 12
Joined: 12-April 19
Member No.: 26,876



Thank you for the reply!

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

This post has been edited by jernatety: Sep 21 2019, 04:52 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Sep 21 2019, 05:18 AM
Post #4


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
jernatety
post Sep 21 2019, 05:24 AM
Post #5


Newbie
*

Group: Members
Posts: 12
Joined: 12-April 19
Member No.: 26,876



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


This post has been edited by jernatety: Sep 21 2019, 05:26 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 05:11 AM