Calculate Time difference or how to write scripts that accomplishs such common tasks as summation,total days of working

there are two fields in form namely start time and end time my goal is to calculate time difference in HH:MM format.so how to fetch data from this field and set value in form field.as there are no tutorial that tells how to fetch values of this docField.i m totally novice to Frappe, i don’t have that much knowledge on it. any help is appreciated.

Thanks,
Hardik

@Hardik_Mehta

frappe.ui.form.on("Form Name", "on_submit", function(frm) { 
  var field_one = frm.doc.field_name;
  var field_two = frm.doc.field_name;
  // your logic here
});
1 Like

@srajelli thnk u so much for your response. what event should i use if i want this to be calculated as soon as user fill up both end time and start time and if end time is greater than start time. can u please help on this.

@Hardik_Mehta

List of events

  • validate
  • before_save
  • after_save
  • before_insert
  • after_insert
  • before_submit
  • before_cancel
  • before_update_after_submit
  • on_update
  • on_submit
  • on_cancel
  • on_update_after_submit

in your case validate will serve the purpose.

2 Likes

@srajelli can u please provide example on validate event? please it will be more helpful for novice like us.

@Hardik_Mehta just replace on_submit with validate

@srajelli ok i will try right now and let you know.

@srajelli below is the form and scenrio.

Thanks :slight_smile:

@Hardik_Gadesha so did it solved your issue ?

No @srajelli .

we are executing your code in this form. we just depict our scenario by this image.

@srajelli i want to calculate time difference as soon as user enters both end time and start time

@Hardik_Mehta You want to calculate time diff and store the output on the third field (Total Time) right ?

@Hardik_Gadesha

endtime:function(frm){
var sd=Date.parse(“2/12/2016”+’ ‘+frm.doc.starttime);
var ed=Date.parse(“2/12/2016”+’ '+frm.doc.endtime);
if(sd>ed)
{
frappe.msgprint(“Start Time Must Be Is Smaller Than EndTime”);
}
}

Thanks, Sukrut
New Indictranstech pvt.ltd

2 Likes

@srajelli exactly i want this.

Thanks @jsukrut. It’s working.

Now could you help me for calculate time difference after this validation ?

Thanks @jsukrut @srajelli for your inputs. :slight_smile:

How can i use this frappe.utils.data.time_diff(string_ed_date, string_st_date) function. can you guide me through this. Can i use this for time difference calculation, if yes then how can i use this ?

Regards,
Hardik Gadesha

@Hardik_Gadesha

to use above method, you need code in controller file ie Doctype .py
eg.
suppose TimeDifference is Doctype Name

class TimeDifference(Document):
def validate(self):
time_diff=frappe.utils.data.time_diff(self.endtime,self.starttime)
self.difference=time_diff

1 Like

@srajelli @jsukrut thank you so much for your help it helps alot.