Calculate total duration spent on a form

How can i calculate total time spend by a user on a particular form. I.e from the form is loaded till it gets submitted.

There is a hidden field in every document called “created”. Just check if the document status is submitted. If it is, compare the “modified” timestamp with the created timestamp to calculate the duration.

If your form has fields which are allowed to be edited after the document has been submitted this will produce change the modified timestamp.

Probably the safest way is to create a custom field for example “submitted_at” as a DateTime. Then register a hook to set this timestamp when the document is submitted. Therefore, you can get your duration by calculating the difference between “created” and “submited_at”.

Thank you for your reply @dj12djdjs.
Currently I have created 3 custom fields - starttime, endtime , duration all type= time, are hidden fields on same doctype which i want to calculate the duration of. I.e employee form
for starttime I’ve default=today. And then I’m using after_save event in custom scripts to set endtime and then access both and do the calculation and then set duration. Is this the right way?
But as you mentioned there’s already a hidden field created. Can i access that hidden field ‘created’ and assign it to a variable so that i can use it in my custom scripting and set duration.

I’m new to frappe so haven’t used hooks. So can you show me how to set this fields auto using hooks and so then i can use them to set the duration using custom scripting.