Calculate age from date from collection

Hi,

Ive been trying this before posting for a long time now and i can’t get my code to work.

I have a collection wich include all sort of information about abandoned buildings (name, year of construction, pictures, age of the building, number of years abandonned, status etc)

One of the page on my site is an index, with a repeater connected to the ‘locations’ collection where all the infos about the buldings are, in a list format.

SO, what i want to acheive is the that the age of the buildings (and year of abandon too) will be calculated automatically, from the construction date wich is just the year, like 1934.
( but if i need it to be a in a date yyyy/mm/dd format i can change it) but would rather just use the year.

I have tried folowing this :

Ive so created a after querry hook from my collection and everything like stated in the how to but no luck… my reapeater end up being empty when i do the preview…

I tried this other option wich worked, but its not meant for a repeater so the same year just repeat itselft:

Here is my code in both of the .js file and the code on my index page.

data.js :
import {ageFromcons} from ‘backend/calculations’;
export function Locations_afterQuery(item, context) {
item.age = ageFromcons(item.cons);
return item;
}

calculation.js:
export function ageFromcons(cons) {
let now = new Date();
let age = now.getFullYear() - cons.getFullYear();
let months = now.getMonth() - cons.getMonth();
let days = now.getDate() - cons.getDate();
if (months < 0 || (months === 0 && days < 0)) {
age–;
}
return age.toString();
}

Code from my index page:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady(function () {
//TODO: write your page related code here…
});

$w.onReady(function () {
$w(“#repeater1”).onItemReady( ($w, itemData, index) => {
$w(“#text6”).text = itemData.age;
} );
} );

export function line3_viewportEnter(event, $w) {
$w(“#StickyMenu”).hide();
}

export function line3_viewportLeave(event, $w) {
$w(“#StickyMenu”).show();
}

import wixData from ‘wix-data’;

// …

export function button7_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“construction”)
);
}

export function button26_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“nom”)
);
}

I would probably need another code for sorting the age column too, because i dont know how i can sort it since it will not be part of the database schema.
My code for sorting my column are example that, (button26 being example ‘construction’ button)
export function button26_click(event, $w) {
$w(“#IndexDataset”).setSort(
wixData.sort()
.ascending(“nom”)
);
}

Sorry for the long tread! haha

Thanks to whoever can help me

Guillaume

Hi Guillaume,
I would use the query function. Note that you need to set the sorted information to the repeater after receiving the results.

You can follow the explanation here but instead of using the Array.filter function, you should change it to Array.sort function.

I hope it’s clear.

Best,
Tal.

Hi,

Thanks but i dont understand… what you just explain is for the sorting of the field that is created by the code? (my second question?)

Because i dont undestand how this is explaining how to calculate the age from dates…

Anyone else can help with some ideas?
Maybe i dont understand but Tal explanation left me clueless lol

Sorry im french too so its harder to understand sometimes.

Hey,
Sorry, I missed your first question. Firstly, I noticed that you’ve added this function which seem to calculate the age:

export function ageFromcons(cons) {
 let now = new Date();
 let age = now.getFullYear() - cons.getFullYear();
 let months = now.getMonth() - cons.getMonth();
 let days = now.getDate() - cons.getDate();
 if (months < 0 || (months === 0 && days < 0)) {
   age--;
 }
 return age.toString();
}

What was the issue with this function?

Moreover, you can check out this “Calculate age given the birth date in the format YYYYMMDD” stackoverflow thread. After calculating the date, you can get the year by using Date.getFullYear function.

I hope it’s clearer now.

Best,
Tal.

I want a code to calculate the age from either a date field (yyyymmdd) or from just a year like 1934 ( just the year would be perfect since i just know the year of the locations)
So i dont have to change the ‘age’ of the buildings in my database each year…

The code ive shared just dont work, my repeater go blank, everything is gone , not just the age field, it looks like this when i do a preview:

Up on my last question

Ive change my reapaeter for my index to a table, but its the same thing, how can a have the age calculated from a year?

Hi @guillaume-clement ,
Can you please share with us the code you’ve written?

Thanks,
Tal.

It’s the smae thing as in the meaasges above, i dont get how to implement it.

Hi,

I am at it again, it’s been a long time im trying to resolve this.
I completely stopped working on my website cause i was exhausted not making this work.

Please, If someone can write that bit of code for me ill pay, im not a coder or anything so it will take me ages to figure this out.

Ive wrote like 3 different tread about this issue and can’t get it to work.

So again, i have 2 date field in a collection wich i want them to be calcultaded to give me the age , that would need to go in another field in my collection.

Both of the date and calculated age have to be in a repeater that is connected to the database.