ERPNext Conference 2019* ERPNext.com Blog

How to get time difference in minutes

timesheets
script
custom_script

#1

Hi,

I have 3 fields as below:

  1. entry_datetime (field type: datetime)
  2. exit_datetime (field type: datetime)
  3. duration (field type: data)

Currently, I am calculating the time difference in hours between entry_datetime and exit_datetime using the below custom script

frappe.ui.form.on("Timesheet", "exit_datetime", function(frm){
    frm.set_value("duration", frappe.datetime.get_hour_diff(frm.doc.exit_datetime , 
frm.doc.entry_datetime));
});

frappe.ui.form.on("Timesheet", "entry_datetime", function(frm){
    frm.set_value("duration", frappe.datetime.get_hour_diff(frm.doc.exit_datetime , 
frm.doc.entry_datetime));
});

I want to now calculate the difference in minutes between the 2 fields. I did not find any method like get_hour_diff to calculate the difference in minutes.

Any help is appreciated :slightly_smiling_face:


#2

You can find solution in below link
https://discuss.erpnext.com/t/sum-two-time-fields/49913/3?u=rohan_jain1


#3

Hi @ROHAN_JAIN1 ,

I tried the code you’ve given in that post but that in that case both the fields are set as “time” types whereas in my case the fields are set as “datetime” fields. Can you please help me with how I can implement your code for my case?

Thanks a lot for your prompt replies.


#4

@shashank_shirke if you got difference in hours . then just multiply with 60
i’am not sure , but try this below code
frappe.ui.form.on(“Timesheet”, “exit_datetime”, function(frm){
frm.set_value(“duration”, frappe.datetime.get_hour_diff(frm.doc.exit_datetime ,
frm.doc.entry_datetime) * 60);
});

frappe.ui.form.on(“Timesheet”, “entry_datetime”, function(frm){
frm.set_value(“duration”, frappe.datetime.get_hour_diff(frm.doc.exit_datetime ,
frm.doc.entry_datetime) * 60);
});


#5

@shashank_shirke this will work for “datetime” fields

var entry_datetime = cur_frm.doc.entry_datetime.split(" ")[1];
var exit_datetime = cur_frm.doc.exit_datetime.split(" ")[1];
var hour=0;
var minute=0;
var second=0;

var splitEntryDatetime= entry_datetime.split(':');
var splitExitDatetime= exit_datetime.split(':');

hour = Math.abs(parseInt(splitExitDatetime[0])-parseInt(splitEntryDatetime[0]));
minute = Math.abs(parseInt(splitExitDatetime[1]) - parseInt(splitEntryDatetime[1]));
hour = hour + minute/60;
minute = minute%60;
second = Math.abs(parseInt(splitExitDatetime[2])-parseInt(splitEntryDatetime[2]));
minute = minute + second/60;
second = second%60;
cur_frm.set_value("duration",hour+':'+minute+':'+second);
cur_frm.refresh_fields("duration");

#6

Hi @ROHAN_JAIN1

I tried your code sample but it’s still not working as expected. It shows some weird values. I am attaching a few screenshots of the same for your reference.

===

===

See how its calculating values in a weird way and also displaying it in a weird way.

I’ll try to explain you the expected functionality with a couple of examples;

Entry Time: 19-09-2019 07:30:00
Exit Time: 19-09-2019 11:00:00
Duration: 210
(difference between 7am and 11:30am in minutes)

Entry Time: 19-09-2019 07:30:00
Exit Time: 21-09-2019 11:00:00
Duration: 3090
(difference between 7am (19-Sep) and 11:30am (21-Sep) in minutes)


#7

Hi @abrarpv97

I have already implemented this solution currently as a workaround but the thing is it does not calculate exact minutes.

The get_hour_diff method calculates the difference in whole integers and that’s the exact reason we want to calculate it in minutes to get exact difference.

For eg.:

Entry Time: 19-09-2019 00:38:24
Exit Time: 19-09-2019 02:16:24
Duration: 60

Entry Time: 19-09-2019 00:09:24
Exit Time: 19-09-2019 04:16:24
Duration: 180

So basically this solution just gets us multiples of 60 as the difference in minutes which is not very accurate for further calculations.


#8

@shashank_shirke
you can try method ‘time_diff_in_hours(frm.doc.exit_datetime , frm.doc.entry_datetime)*60’ instead of ‘get_hour_diff(frm.doc.exit_datetime , frm.doc.entry_datetime)*60’ . hope it will work…
OR
you can refere jobcard doctype in manufacturing module . i think you will get an exact solution there .
folder path /apps/erpnext/erpnext/manufacturing/doctype/job_card/job_card.py


#9

@shashank_shirke ,

        var entry_datetime = cur_frm.doc.entry_date_time.split(" ")[1];
		var exit_datetime = cur_frm.doc.exit_date_time.split(" ")[1];
		var splitEntryDatetime= entry_datetime.split(':');
		var splitExitDatetime= exit_datetime.split(':');
		var totalMinsOfEntry= splitEntryDatetime[0] * 60 + parseInt(splitEntryDatetime[1]) + splitEntryDatetime[0] / 60;
		var totalMinsOfExit= splitExitDatetime[0] * 60 + parseInt(splitExitDatetime[1]) + splitExitDatetime[0] / 60;
		var entry_date = new Date(cur_frm.doc.entry_date_time.split(" ")[0]);
		var exit_date = new Date(cur_frm.doc.exit_date_time.split(" ")[0]);
		var diffTime = Math.abs(exit_date - entry_date);
		var diffDays = Math.ceil(diffTime/ (1000 * 60 * 60 * 24));
		cur_frm.set_value("duration",parseInt(((diffDays*(24*60)) +totalMinsOfExit) - totalMinsOfEntry));
		cur_frm.refresh_fields("duration");

Screenshot%20from%202019-09-19%2014-33-10


#10

Sad to say this but this code is still doesn’t seem to work. I think there is some confusion in the field names and the variable names.

To clarify it, the field names and types are as below:

1. entry_date_time (type: datetime)
2. exit_date_time (type: datetime)
3. duration (type: data)

Below is the code that I’ve put up which doesn’t seem to work

frappe.ui.form.on("Timesheet", "exit_date_time", function(frm) {
	var entry_datetime = frm.doc.entry_date_time.split(" ")[1];
	var exit_datetime = frm.doc.exit_date_time.split(" ")[1];
	var splitEntryDatetime = entry_datetime.split(':');
	var splitExitDatetime = exit_datetime.split(':');
	hourInMinOfEntry=splitEntryDatetime[0] * 60;
	hourInMinOfExit = splitExitDatetime[0] * 60;
	secondInMinOfEntry = splitEntryDatetime[0] / 60;
	secondInMinOfExit = splitExitDatetime[0] / 60;
	totalMinsOfEntry=hourInMinOfEntry+parseInt(splitEntryDatetime[1]) + secondInMinOfEntry;
	totalMinsOfExit=hourInMinOfExit+parseInt(splitExitDatetime[1]) + secondInMinOfExit;
	var entry_date = new Date(frm.doc.entry_date_time.split(" ")[0]);
	var exit_date = new Date(frm.doc.exit_date_time.split(" ")[0]);
	var diffTime = Math.abs(exit_date - entry_date);
	var diffDays = Math.ceil(diffTime/ (1000 * 60 * 60 * 24));
	var DaysDiffInMins=diffDays*(24*60);
	result=(DaysDiffInMins+totalMinsOfExit) - totalMinsOfEntry;
	frm.set_value("duration",parseInt(result));
});

I have just modified to cur_frm to just frm as I think the former is deprecated

Also I have removed the last line of refresh_fields since I have set the action as exit_date_time in the first line


#11

@shashank_shirke, Please Refer Edited version for reference.


#12

This is working perfectly now.

Thank you! :grin: :clap: