Q:

How can I find the difference between two time fields and add the result in the same document, inside a collection at MongoDB?

I am using the below function to add one row to the table. If user click Add-row button the below function will get called and one row will be added. In that row user have to enter start and end time .
Now my question is how to calculate the total time from that start and end time like we will calculate in Excel. 
I know how they are doing in Excel but how to do the same thing in the table like this ?
I am using node + MongoDB for rendering pages.
var count=0;
function addRow(tableID) {
var id = "newlyaddedrow" + count;
var users = document.getElementById(tableID);

var row = `<tr class="info" style="cursor: pointer;background-color: #dbedf8;" id="${id}">
    <td><input id="workAllocation_DateID"  type="date" class="form-control"  name="@date"                   value=""/></td>         
    <td><input type="text" class="form-control"  name="@project_ID"        value=""/></td>             
    <td><input type="text" class="form-control"  name="@issue_Summary" value=""/></textarea></td>         
    <td><input type="text" class="form-control"  name="@short_Description"  value=""/></textarea></td>
    <td><input type="time" class="form-control"  name="@start_Time"            value=""/></td>
    <td><input type="time" class="form-control"  name="@end_Time"            value=""/></td>     
    <td><input type="time" class="form-control"  name="@total_Time"            value=""/></td>         
</tr>  `;

count++;

}
Below structure I have in mongoose collection. 
And I want to calculate that total_time when the user enter start_time and end_time.
project_ID:"xxxx"
issue_Summary:"aaaa"
short_Description:"aaaa"
start_Time:"02:02"
end_Time:"03:02"
total_Time:""
_id:5d0ca14e138a7628948804af
date:2019-06-21 05:30:00.0001

Is there any way to do this? Can someone help me in this ? 

A:

I have tried with event and it's working perfect. 
This event is happening onblur() of the that particular column in the row
function calculateTime(){
var startTime = new Date();
var endTime = new Date();
var totalTime = new Date();     
var startTimeStr = '';   
var endTimeStr = '';
var totalTimeStr = '';
if(event.target.name == '@start_Time' && event.target.value != ''){
    startTimeStr = event.target.value;
    if((event.target.parentElement.nextElementSibling.firstElementChild).value != ''){
        endTimeStr = (event.target.parentElement.nextElementSibling.firstElementChild).value;
    }     
}else if(event.target.name == '@end_Time' && event.target.value != ''){
    endTimeStr = event.target.value;
    if((event.target.parentElement.previousElementSibling.firstElementChild).value != ''){
        startTimeStr = (event.target.parentElement.previousElementSibling.firstElementChild).value;
    }  
    totalTimeStr = (event.target.parentElement.nextElementSibling.firstChild);
}
if(startTimeStr != '' && endTimeStr!= ''){
    startTime = startTime.setHours(startTimeStr.split(':')[0],startTimeStr.split(':')[1]);
    endTime = endTime.setHours(endTimeStr.split(':')[0],endTimeStr.split(':')[1]);
    if(startTime > endTime){
       alert('End time cannot be before start time!!!');
    }else{
        totalTime = endTime - startTime;
        var hours = Math.floor(((totalTime / (1000*60*60)) % 24));
        var minutes = Math.floor(((totalTime / (1000*60)) % 60));
        totalTimeStr.value = hours +':'+ minutes;
        console.log('Total Time' +totalTimeStr);
    }
}

}

