Script to calculate total days between tow date in child table

I have to field in chield table I need a script to calculate total days in third field

Could you please elaborate?
you have a field in the child table that you wish to update when modifying another field in this the same row?

Yes, exactly

Then you’ll need something like this:

frappe.ui.form.on('Project Task', {
    date1: function(frm, cdt, cdn){
        var d = locals[cdt][cdn];
        if (!d.date1) return;
        // put any logic here
        var diff_day = d.date1 - d.date2
        // update the field
	frappe.model.set_value(cdt, cdn, 'end_date', diff_day);
    }
});

So basically, when you’ll set the date1, the function will be triggered. you can access any information in that particular row using the variable ‘d’'.
Then you do the calculation you need to do and finally update the value in the row by using the frappe.model.set_value() for the field ‘end_date’.

I tried this script but not worked

frappe.ui.form.on('Plan Tasks', "refresh", function(frm, cdt, cdn){
var d = locals[cdt][cdn];   
frappe.model.set_value(cdt, cdn, 'total_days', frappe.datetime.get_day_diff( d.to_date, d.from_date));
    refresh_field('total_days');
});

it won’t because the event is on ‘refresh’ instead of being on the field.

frappe.ui.form.on('Plan Tasks', {
    to_date: function(frm, cdt, cdn){
        var d = locals[cdt][cdn];
        if (!d.to_date) return;
        // put any logic here
        var diff_day = frappe.datetime.get_day_diff(d.to_date, d.from_date);
        // update the field
	frappe.model.set_value(cdt, cdn, 'total_days', diff_day);
    }
});
2 Likes

Thanks, Its worked after Delete one of the brackets from ( d.to_date, d.from_date))

please edit so mark it as Solution

Meny Thanks

@Akram_Mutaher

I need your help

@Hardik_Gadesha , @Mohamed2335

any help please doesn’t work with me I don’t know why

can you share your script?

It appears when the first date is placed, and the zero does not change even after adding the second date


@Akram_Mutaher

@Nada-86
Try the script below and change the names of fields with your field names start_date , end_date , total_days

frappe.ui.form.on('Training Event2', {
	end_date(frm){
	    var diff_in_days = frappe.datetime.get_diff(frm.doc.end_date, frm.doc.start_date);
        frm.set_value('total_days', diff_in_days);
	}
})
1 Like

Thank uuuuu