To calculate duration type field in ui pages

Poorva Bhawsar
Mega Sage

Hi Community,

 

I have a ui page where i have created a popup and calculating or mapping time worked field with the field i have created using html. Only pasting the html code for time worked field.

 

HTML:

<div id="element.task_time_worked.time_worked" class="form-group " style="">
                        <div class="" data-type="label" choice="0" type="glide_duration" id="label.task_time_worked.time_worked" nowrap="true">
                            <label onclick="return labelClicked(this);" for="sys_display.task_time_worked.time_worked" class="col-xs-12 col-md-1_5 col-lg-2 control-label" dir="ltr">
                                <span id="status.task_time_worked.time_worked" aria-label="" data-dynamic-title="" mandatory="true" oclass="" class="required-marker label_description"></span>
                                <span title="" class="label-text" data-html="false" data-original-title="" aria-expanded="false">Time Worked</span>
                            </label>
                        </div>
                        <div class="col-xs-10 col-md-9 col-lg-8 form-field input_controls">
                            <div ng-non-bindable="" class="hidden">
                                <input type="hidden" id="sys_original.task_time_worked.time_worked" name="sys_original.task_time_worked.time_worked" value=""></input>
                            </div>
                            <div class="row">
                            <span id="task_time_worked.time_worked_entry">
                                <div class="col-xs-5 col-sm-12 col-md-12 col-lg-4 no-right-padding-lg duration_day">
                                    <div class="input-group input-group-1">
                                        <span class="input-group-addon">
                                            <label for="ni.task_time_worked.time_workeddur_day">Days</label>
                                        </span>
                                        <input value="00" type="text" name="ni.task_time_worked.time_workeddur_day" data-type="task_time_worked.time_worked_duration_field" id="ni.task_time_worked.time_workeddur_day" title="Days" class="form-control accessibility_no_tooltip" aria-describedby="label.task_time_worked.time_worked"></input>
                                    </div>
                                </div>
                                <div class="col-xs-7 col-sm-12 col-md-12 col-lg-8 no-left-padding-lg">
                                    <div class="input-group input-group-3">
                                        <span class="input-group-addon">
                                            <label for="ni.task_time_worked.time_workeddur_hour">Hours</label>
                                        </span>
                                        <input value="00" type="text" name="ni.task_time_worked.time_workeddur_hour" data-type="task_time_worked.time_worked_duration_field" id="ni.task_time_worked.time_workeddur_hour" title="Hours" maxlength="2" class="form-control accessibility_no_tooltip" aria-describedby="label.task_time_worked.time_worked"></input>
                                        <span class="input-group-addon sr-only">
                                            <label for="ni.task_time_worked.time_workeddur_min">Minutes</label>
                                        </span>
                                        <input value="00" type="text" name="ni.task_time_worked.time_workeddur_min" data-type="task_time_worked.time_worked_duration_field" id="ni.task_time_worked.time_workeddur_min" title="Minutes" maxlength="2" class="form-control accessibility_no_tooltip" aria-describedby="label.task_time_worked.time_worked"></input>
                                        <span class="input-group-addon sr-only">
                                            <label for="ni.task_time_worked.time_workeddur_sec">Seconds</label>
                                        </span>
                                        <input value="00" type="text" name="ni.task_time_worked.time_workeddur_sec" data-type="task_time_worked.time_worked_duration_field" id="ni.task_time_worked.time_workeddur_sec" title="Seconds" maxlength="2" class="form-control accessibility_no_tooltip" aria-describedby="label.task_time_worked.time_worked"></input>
                                    </div>
                                </div>
                            </span>
                        </div>
                        </div>
                        <div class="col-xs-2 col-md-1_5 col-lg-2 form-field-addons"></div>
 
Client Script:
function update_ticket() {
    var usr = g_user.userID;
    var action = gel('actions').value;
    var response = gel('call_response').value;
    var days = gel('ni.task_time_worked.time_workeddur_day').value;
    var hours = gel('ni.task_time_worked.time_workeddur_hour').value;
    var minutes = gel('ni.task_time_worked.time_workeddur_min').value;
    var seconds = gel('ni.task_time_worked.time_workeddur_sec').value;
    var comments = gel('task_time_worked.comments').value;
    var time_worked = calculateTimeWorked(days, hours, minutes, seconds);  
    var tck = g_form.getUniqueValue();
   

    var gr = new GlideRecord('task_time_worked');
    gr.initialize();
    gr.setValue('user', usr);
    gr.setValue('action', action);
    gr.setValue('u_call_response', response);
    gr.setValue('time_worked', time_worked);
    gr.setValue('comments', comments);
    gr.setValue('task',tck);
    gr.insert();
    GlideDialogWindow.get().destroy();
}

function calculateTimeWorked(days, hours, minutes, seconds) {
    // Convert everything to seconds and sum up
    var totalSeconds = parseInt(days) * 86400 + parseInt(hours) * 3600 + parseInt(minutes) * 60 + parseInt(seconds);
    return totalSeconds; // You can format this as needed
}
 
Whenever i am clicking on a ui action on incident form and filling all the details on the popup. Its creating a record in time worked table. But time worked calculation is wrong.
 
PoorvaBhawsar_0-1701154270904.png

 

What is the logic to calculate correct duration.

 

Thanks,

Poorva

1 REPLY 1

Tai Vu
Kilo Patron
Kilo Patron

Hi @Poorva Bhawsar 

You need to return the duration in the format "ddd hh:mm:ss" instead of total seconds to be able to set value for a duration field type.

 

Ref: Calculate difference bet two dates and set duration.

 

Cheers,

Tai Vu