I am tryin to get the age of the owners, I have a included a date field called “Birthday” and I wanted to calculate the age for each Owner its birthday.
What I did:
Add in “Before tag” the code
Add embed code in the page the same code
<script>
const anos = Birthday
function calculateAge(date)
{
const now = new Date();
const diff = Math.abs(now - date );
const age = Math.floor(diff / (1000 * 60 * 60 * 24 * 365));
return age
}
calculateAge(anos)
</script>
The result does not appear anywhere.
Could anyone help me on that? Basically to bring the results of Javascript into the page.
<script>
const anos = new Date("+Add Field - Birthday HERE");
const now = new Date();
const diff = now-anos;
const age = Math.floor(diff/31557600000);
console.log(age)
document.querySelector('.ages').innerHTML = age;
</script>
It will display 0 if they are less than a year old obviously. Try publishing and testing it out on Eino Ratke.
How can i make this work for all my cms items. For me it only works on the first one. I display a list of tenants per flat, and it will only calculate the age of the first person in the list. How can i fix this, is it posstible or do i need to do something different?
Try adding this code to the bottom of Page Settings > Footer Code:
<script>
const list = document.querySelectorAll('.dynamic-item')
for (i = 0; i < list.length; i++){
let alder = new Date(list[i].querySelector(".leietakeralder > h6.alder").innerHTML);
let now = new Date();
let diff = now-alder;
let age = Math.floor(diff/31557600000);
list[i].querySelector("#leietaker > h6.trip").innerHTML = age;
}
</script>
I’d recommend removing the HTML embeds from the collection items to ensure there’s no clashing of variables.
WOW! It worked, thank you so much. Been a hard week trying different methods to get this to work, and then you do your magic and fixes everything. Thank you so much! And you are so fast, amazing! Have a wonderful day!
Sorry, I need help for dummies. I can’t seem to get it to work. What do I have to do (step by step) to get this div called Birthday to calculate into an Age?
Add a div/text block with the id ‘age’ where you want the age to show up (demo here)
Replace the code in Page Settings > Custom Code > Before </body> tag with the below:
<script>
const list = document.querySelectorAll('.w-dyn-item')
for (i = 0; i < list.length; i++){
let birthday = list[i].querySelector(".birthday").innerHTML
let from = birthday.split(".");
let birthdateTimeStamp = new Date(from[2], from[1] - 1, from[0]);
let now = new Date();
let diff = now-birthdateTimeStamp;
let age = Math.floor(diff/31557600000);
list[i].querySelector("#age").innerHTML = age;
}
</script>
Sorry! I see - please replace code in Page Settings > Custom Code > Before </body> tag with the below on Models template page:
<script>
let birthday = document.querySelector(".birthday").innerHTML
let from = birthday.split(".");
let birthdateTimeStamp = new Date(from[2], from[1] - 1, from[0]);
let now = new Date();
let diff = now-birthdateTimeStamp;
let age = Math.floor(diff/31557600000);
document.querySelector("#age").innerHTML = age;
</script>
Hi, I’m trying to use your code to make a date display an age but I’m not sure how to use your code and have tried implementing and failed, could you help?
This is the code I setup in an imbed, I thought it might display the code in the age in the imbed but it did nothing so I created a text block called age to see if this code would change that but it didn’t. I’d love an example of how to implement your code, Thank you for the help!