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 please please
cur_frm.set_value(AgeField, moment().diff(cur_frm.doc.DATEFIELDNAME, 'years'));
cur_frm.refresh_field(AgeField)
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!
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ā)
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!
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?
@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ā)
}
});
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.
I should start paying you
Thank you again Rohan!