- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2025 10:11 AM
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..?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2025 10:29 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2025 08:03 PM
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
}
..
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.
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2025 10:07 PM
Hello @Mannam Praveen ,
Please check the below given example to solve your query.
If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.
Thanks,
Vaibhav Nikam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2025 10:29 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-02-2025 06:50 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader