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 how to fetch data from this field and set value in form 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.



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.


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.


@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 ?


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

Thanks, Sukrut
New Indictranstech


@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, 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 ?

Hardik Gadesha


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

class TimeDifference(Document):
def validate(self):,self.starttime)

1 Like

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