Number of Days Between two dates script

Hello comunity, what scrip can i use for calculating Number of Days Between two dates ex date1 . date2 and returning the value to a field. Thanks.

Hi @Randy_Lowery

In python, you can use date_diff function to calculate number of days between two dates

How to use it

from frappe.utils import date_diff
date_diff(end_date, start_date)

For more details please check below link

3 Likes

Hello @rohit_w thank you for the feedback, i was trying to use this scrip

 frappe.ui.form.on("Liquidacion", "refresh", function(frm){
    frm.set_value("periodo_total_laborado", frappe.datetime.get_day_diff( d.fecha_salida , d.fecha_ingreso ));
});

using the frappe.datetime.det_day_diff funtion under frappe/frappe/public/js/frappe/misc/datetime.js

but cant mange to make it work, can you please advice what could be wrong?

The code is correct, can you check the type of input whether it’s date type or string

1 Like

hi i set both fields as date in the Doctype, should it be set as string? for now only works when i enter the dates directly in the scrip and no in the fields for example

frappe.datetime.get_day_diff( β€œ2016-07-05”, β€œ2016-05-03” ).

Yes this is correct no need to set it as date, I think there is some other issue.
Understand the issue, is that the fields are in child table? Because in below code you have used d.fieldname where d is not defined.

frappe.ui.form.on("Liquidacion", "refresh", function(frm){
    frm.set_value("periodo_total_laborado", frappe.datetime.get_day_diff( d.fecha_salida , d.fecha_ingreso ));
});

no both fields belongs to the parent doc-type, i had used same type scrip for calculation before and i know it uses the same syntax

In that case, the code should be like frm.doc.fecha_salida and frm.doc.fecha_ingreso

3 Likes

Thankyou @rohit_w for your awesomeness support finally make it work the scrip for anyone that may find it usefull is:

frappe.ui.form.on("DOCTYPE", "refresh", function(frm, cdt, cdn){
var d = locals[cdt][cdn];   
frappe.model.set_value(cdt, cdn, "FIELD", frappe.datetime.get_day_diff( d.LAST_DATE , d.FIRTS_DATE));
    refresh_field("FIELD");
});
6 Likes

This topic was automatically closed after 24 hours. New replies are no longer allowed.