I want to show my website’s members information in dynamic pages automatically from a database, like photo / name / birth / age / etc.
But I can not find the way to show the age… because age has to be changed day by day.
so if it’s possible to use a formula like excel (DATEDIF(“2003/4/7”,“2014/2/12”,“Y”)) in wix code, it’s very nice, but it seems to be impossible.
Does someone know how to do this or some other way to show age automatically?
Please help.
One way to do this is to use the dynamic page’s dataset’s onReady function to register an event handler that will get the member’s date of birth, calculate the proper age, and put that age as an element’s value.
So something like this:
$w.onReady(function () {
$w("#dynamicDataset").onReady( () => {
let dob = $w("#dynamicDataset").getCurrentItem().dob;
let age = // calculate age based on dob
$w("#age").text = age;
} );
} );
Another way to do this is to create an afterRouter hook on your dynamic page. Inside the hook you can get the member’s date of birth and calculate the proper age. You can send that data to the page.
To learn about data binding router hooks, see About Data Binding Router Hooks and Creating a Data Binding Router Hook.
Another way of doing so is using database hooks.
You start by defining an age field of type number.
Then, you click on hooks and add an afterQuery hook, at which you calculate the age and set the value to the field.
For instance, it can look something like the following
export function members_afterQuery(item, options) {
// getting the age in milliseconds from epoch
var ageDifMs = Date.now() - item.birthday.getTime();
// milliseconds from epoch
var ageDate = new Date(ageDifMs);
// converting the milliseconds to an age
item.age = Math.abs(ageDate.getUTCFullYear() - 1970);
// we have to return the item when using hooks
return item;
}
The code was taken from StackOverflow