Convert days, Hours, Minutes into Hours set Value No of hours in time spent in hours field

Mannam Praveen
Tera Expert

I have to two field on the form. one field is time worked Field type is - Duration and other field is Time Spent hours field type string or decimal

My Requirement whenever user provided no of days or hours, minutes worked in the time worked field. it should calculate that hours update the value in the Time Spent hours field how to achieve this..?

 

MannamPraveen_0-1738174303088.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mannam Praveen  

you can use before update BR and condition

Time Worked field Changes

Script: check these 2 links

converting a duration to hours 

Convert Business duration into hours. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

hi @Mannam Praveen  ,

 

  • Use a Client Script:

    • A Client Script can capture changes in the Time Worked field and calculate the total hours dynamically.
  • Script Example:

 

// Client Script Configuration
// Type: onChange
// Table: Your desired table (e.g., Incident)
// Field: Time Worked

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Exit if the form is loading or if the field is empty
    }

    // Get the value from the Time Worked field (duration field)
    var timeWorked = g_form.getValue('time_worked'); // Replace 'time_worked' with the actual field name

    // Parse the duration value (format: "days hours:minutes:seconds")
    var durationArray = timeWorked.split(' ');
    var days = 0, hours = 0, minutes = 0;

    if (durationArray.length === 2) {
        days = parseInt(durationArray[0] || 0, 10); // Extract days
        var timeArray = durationArray[1].split(':'); // Split hours, minutes, seconds
        hours = parseInt(timeArray[0] || 0, 10);
        minutes = parseInt(timeArray[1] || 0, 10);
    }

    // Calculate total hours
    var totalHours = (days * 24) + hours + (minutes / 60);

    // Update the Time Spent Hours field
    g_form.setValue('time_spent_hours', totalHours.toFixed(2)); // Replace 'time_spent_hours' with your field name
}

 

 

..

  1. Time Worked Field Parsing:

    • The timeWorked.split(' ') splits the duration into days and time parts.
    • Days are extracted from the first part, and the time (hours:minutes:seconds) is further split to calculate total hours.
  2. Total Hours Calculation:

    • (days * 24) converts days to hours.
    • (minutes / 60) converts minutes to hours.
    • The result is added up to form the total hours.
  3. Update the Time Spent Hours Field:

    • The g_form.setValue() function sets the calculated hours into the Time Spent Hours field.

 

  • Ensure that the Time Worked field uses the duration field type (days hh:mm:ss format).
  • Verify that the Time Spent Hours field is a string or decimal type capable of storing the calculated value.

 

 

Vaibhav_Nikam
Tera Guru

Hello @Mannam Praveen  ,

Please check the below given example to solve your query.

var gr = new GlideRecord('incident');
gr.addQuery('sys_id','e329de99731423002728660c4cf6a73c');
gr.query();
if(gr.next())
{
var openedDate = gr.getValue('sys_created_on');
gs.info(openedDate);
var closedDate = gr.getValue('closed_at');
gs.info(closedDate);
var gdt1 = new GlideDateTime(openedDate);
var gdt2 = new GlideDateTime(closedDate);
var dur = new GlideDateTime.subtract(gdt1, gdt2);
gs.info(dur.getDisplayValue());
var hours = dur.getNumericValue() / 1000 / 60 / 60// convert to hours
gs.info(hours);
}
 
vaibhavnikam_0-1738217263646.png

 


If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.

Thanks,

Vaibhav Nikam

Ankur Bawiskar
Tera Patron
Tera Patron

@Mannam Praveen  

you can use before update BR and condition

Time Worked field Changes

Script: check these 2 links

converting a duration to hours 

Convert Business duration into hours. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Mannam Praveen  

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader