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.


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:


cur_frm.set_value(AgeField, moment().diff(cur_frm.doc.DATEFIELDNAME, 'years'));

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


For child Table,
cur_frm.doc.CHILDTABLE_FIELD_NAME[index].AGEFIELD=moment().diff(cur_frm.doc.DATEFIELDNAME, ‘years’)


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?


Here you go :smiley:

frappe.ui.form.on(“Employee Children”, {
birthday:function (frm,cdt,cdn) {
locals[cdt][cdn].age=moment().diff(locals[cdt][cdn].birthday(), ‘years’);


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.


I should start paying you :stuck_out_tongue_winking_eye:

Thank you again Rohan!