Hello Readers!

I have been asked so many times by my customers how to see the time spent on an activity if one has entered the ‘Start Time’ & ‘Finish Time’. It’s easy and simple. This time calculation can also be used for calculating total time spent on a case resolution if we replace ‘Start Time’ with ‘Created on’ and ‘Finish Time’ with ‘Actual Resolution on’, and so on and so forth.

There can be multiple usage of this JavaScript based on your time calculation requirements. In this scenario, I am taking up a custom activity called ‘Repair Activity’ as shown in the following screenshot:

Calculating Time Spent on an Activity via JavaScript in Dynamics 365


Please note that the Time Spent field is a decimal field.

Steps for Calculating Time Spent on an Activity

Step 1: Write the JavaScript code as shown below:

Step 2: Add a webresource in Dynamics CRM.

Add a new web resource by typing in the name, display name, language & type > Save & Publish > URL will be generated.

new web resource

Step 3: Add this web resource to form Libraries.

Go to ‘Repair Activity’ Form Editor > Form Properties > Form Libraries > Add.

add web resource


Also manage the ‘Event Handler’ by setting the event when you want this JavaScript to be executed, so I will set it on ‘On Save’ and set control to ‘Form’, then click OK.

event handlers


Once done, save the form and publish it.

That’s it!

Here you go. Now you should be able to see the time spent as soon as you save the form. This JavaScript also gives us an alert if we have not provided any value in Start & Finish time.

new repair activity

repair activity information

Happy CRM’ing!