The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> 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
 
Reply to this topicStart new topic
Replies
Christian J
post Sep 21 2019, 05:18 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,653
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 #3


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

Posts in this topic


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

 



- Lo-Fi Version Time is now: 19th April 2024 - 07:50 PM