Post

3 followers Follow
0
Avatar

Javascript for counting time taken to fill a survey

I have created an app for a very detailed and long survey in our company. In this app, I have a category field called Survey Status, which is a required field and has two options "In Progress" and "Completed". By default "In Progress" gets selected while creating a new entry in the app. This category field is the last field in the survey app. So once people complete the survey, they will mark it as "Completed".
Assuming people will fill up the survey in one sitting, I would like to have a calculation field that automatically calculates the time taken to fill up all the fields from start to end. I have seen many javascript examples for calculating days automatically and tried to modify it in my app to count hours, but either it doesn't work or it gives me wrong results.
The idea is that people will select the first field called "name" and then save the entire form. This will give me the moment(@Created On) value. Then they will open it again and start filling up the remaining fields until the last field called Survey Status where they will change it to "Completed". It can be used to capture the end time for the survey.

This is the javascript that I am playing with currently:

var start = moment(@Created On);
var end = moment(@Survey Status=='Completed');
start.diff(end,'hours')

Can somebody suggest me a better way to do this? Maybe my entire thought process is incorrect.

Anish Pillai Answered

Please sign in to leave a comment.

2 comments

1
Avatar

Hi Anish,
this code should work. It grabs the end-time and does the calculation when the Status changes to "Completed" . As long as the Status is "In Progress" it shows nothing (empty field). It must be end.diff(start...) , with start.diif(end ...) you'll get a negative number. I would take minutes/60 as unit, cause it will deliver a more precise value.

var start = moment(@Created On);
var end = moment();
@survey status == "Completed" ? end.diff(start,'minutes')/60 : ""

Rainer
rg@delos-consulting.com

Rainer Grabowski 1 vote
Comment actions Permalink
0
Avatar

Thank you so much Rainer. It works the way I wanted it.

Anish Pillai 0 votes
Comment actions Permalink