Time Difference

How to get difference of two time fields?

Please help.

This is more of a python problem, you’re better off doing a Google search for this, but there are functions in frappe that allows you to do this easily.

Import the functions like : from frappe.utils import time_diff

Can we create custom Script to calculate time difference.

Please help.

yes you can create a custom python script and import time_diff as suggested by @vjFaLk

Ravindra, Can you please send the written code, so that I can paste it in custom script(client).

I’m really weak in coding and all.


Try with this,

from frappe.utils import time_diff_in_hours
diff = time_diff_in_hours(“05:22:00”, “14:30:14”)
Print diff

I have written code like below.

frappe.ui.form.on(‘Issue Case History’, ‘validate’, function(frm){

from frappe.utils import time_diff_in_hours

var diff;

diff=time_diff_in_hours(frm.doc.end_time, frm.doc.start_time);

cur_frm.set_value(“total_time”, diff);


Note: here “Issue Case History” is a child table to “Issue”.

This code is not working for me.

Kindly help.


We are using cloud based ERPNext.

So is it possible to use these python scripts in client side custom scripts? below code doest
work. Please help.

frappe.ui.form.on(“Issue Case History”, “start_time”, function(frm, cdt, cdn) {
var z = locals[cdt][cdn];

from frappe.utils import time_diff_in_hours
z.total_time=time_diff_in_hours(z.end_time, z.start_time);


Create a custom script report for document “Issue” and use following code there:

frappe.ui.form.on("Issue Case History", "start_time", function(frm, cdt, cdn) {
	var z = locals[cdt][cdn];

	if(z.start_time && z.end_time) {
		total_time = frappe.datetime.get_hour_diff(z.end_time, z.start_time);
		frappe.model.set_value(cdt, cdn, "total_time", total_time);

I use this code to calculate the time difference

frappe.ui.form.on('Attendance', {
refresh: function (frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    console.log(cdt, cdn, d.time_in, d.time_out, d.total_hours);

    if (d.time_in && d.time_out) {
        var total = frappe.datetime.get_hour_diff(d.time_out, d.time_in);
        frappe.model.set_value(cdt, cdn, 'total_hours', total);
        console.log(d.time_in, d.time_out, d.total_hours);

It returns NaN for the total.
If I don’t use var for total (as in your code), it returns undefined in the console.

Can you please tell me where I do wrong?
Thank you

After press enter to post previous message, I was thinking about changing the field format (previously they use Time).
So I changed it to Datetime (all 3: time_in, time_out, total hours) and it calculates. Even if it’s wrong.

In the console the total_hours returns 7. And this

Another try. Use time_in as Datetime, time_out as Datetime, and total_hours as Time. Now it calculates hours correctly.

So I was wondering if the get_hour_diff is working as it should be.

Secondly, how do I calculate the minute as well?

Thank you

1 Like

I use this code for calculate Total Working Hours.
NB: Use time_in as Time, time_out as Time, and total_hours as Float Type

frappe.ui.form.on('Attendance', {
refresh: function (cur_frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    console.log(cdt, cdn, d.in_time, d.out_time, d.total_working_hours);

    if (d.in_time && d.out_time) {
        var in_times = parseFloat(cur_frm.doc.in_time);
        var out_times = parseFloat(cur_frm.doc.out_time);
        var total = parseFloat(out_times - in_times);
        frappe.model.set_value(cdt, cdn, 'total_working_hours', total);
        console.log(d.in_time, d.out_time, d.total_working_hours);



Hey Guys i want to calculate total time a person spends on a form. i.e from time form is loaded till the form is submitted. So how can i get start time of the form and whats the best way to approch this problem.