# Calculate Age for a Student

@James_Robertson use the āRead Onlyā fieldtype! itās not stored in database, and you can customize they value using javascript

In addition to @max_morais_dmmās answer, you can use Moment.js library for calculating the age of the Student or can refer the datetime.js file in the frappe.

Thanks @max_morais_dmm and @ManasSolanki. That makes sense. So I am not a programmer type and donāt know javascript. Assuming I can figure that out, where on the doctype form do I place said code to have the field get calculated?

I essentially want to take the out of box date_of_birth field and divide it by 365.25 to give me the age in years as a whole number/integer.

Thoughts?

The date_of_birth field is of type ādateā.
You can use:
var now = moment();
var total_days = moment(now).diff(date, ādaysā);
var number_of_years = Math.floor((total_days).toFixed(2) / 365.25);

@iMoshi

``````cur_frm.set_value(AgeField, moment().diff(cur_frm.doc.DATEFIELDNAME, 'years'));
cur_frm.refresh_field(AgeField)``````
2 Likes

OMG, thank you so much! Just had to add semicolons and apostrophes you are a life saver!

However, this code is not working on child tables. Any help on thatā¦?

Thank you again!

1 Like

@iMoshi

For child Table,
cur_frm.doc.CHILDTABLE_FIELD_NAME[index].AGEFIELD=moment().diff(cur_frm.doc.DATEFIELDNAME, āyearsā)
cur_frm.refresh_fields(āCHILDTABLE_FIELD_NAMEā)

2 Likes

You can create your custom fields(i.e read only and other fields) using customize form , have to write custom script to calculate the age (@ROHAN_JAIN1)has given . Export custom fields and custom script to app using fixtures. If you update doctypes your custom changes will be earsed , by using fixtures you can pull from repository to reflect your changes in your current system

Man, just canāt get it to workā¦ Is hitting āAdd script for Child Tableā the right thing?

I have ābirthdayā date field and would like to have āAgeā (child_age) read-only field to calculate Please take a look at the screen shot

Thank you so much!

1 Like

Man, āIām all inā deep. Got the dev mode on, did lots of changes to many DocTypesā¦ Canāt go back

Iāve learned to not mess with the standard DocTypes, but we have really specific differences in my country.

Can you please share screenshot of child doctype too?

Here you go

@iMoshi
frappe.ui.form.on(āEmployee Childrenā, {
birthday:function (frm,cdt,cdn) {
locals[cdt][cdn].age=moment().diff(locals[cdt][cdn].birthday(), āyearsā);
cur_frm.refresh_fields(āChildTableNameā)
}
});

2 Likes

I somehow canāt get it to work. Fiddled with it a lot, no go ERPNext throws bunch of JS error no matter what I doā¦ Iām just going to hide it for now.

Thank you so much Rohan!

I came across another question.

Can we calculate ādurationā of a āstart dateā and āend dateā in a child table?

Any help is appreciated.

Thanks guys!

Yes we Can calculate it.

2 Likes

I should start paying you

Thank you again Rohan!

Thank you so much for this screenshot. It worked for me!