Calculate Age for a Student

I want to add a read only calculated field that gives me the age of a student in years. The value should be dynamic in the sense that it should be calculated at display and not really stored in the database. Any guidance on what field type to use? Also, is there a TODAY() type function that can be used as well?

@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);

Please help me calculate ā€œageā€ of just about anything thatā€™s given in a ā€œdateā€ field :smiley: please please :smiley:

@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 :smiley: you are a life saver!

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

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 :smiley:

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 :stuck_out_tongue_winking_eye:

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?

:slight_smile:

Here you go :smiley:

@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 :frowning: 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? :smiley:

Any help is appreciated.

Thanks guys! :heart:

Yes we Can calculate it.

2 Likes

I should start paying you :stuck_out_tongue_winking_eye:

Thank you again Rohan!