Convert duration to hours as integer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2016 08:49 AM
Hello,
I'm trying to convert a glide duration (02:00:05) field to an integer representing the hours. I've been trying some different things and I haven't been able to get it to work. Right now I have a client script that sends the form fields to an system include script which is supposed to calculate the time in milliseconds and then the client script converts it to hours. (right now I have it just setup to return the answer in milliseconds for testing)
Understand I'm not very proficient in java script so it could be a very simple mistake I'm making. I appreciate any help I can get!
Thank you!
Here are the scripts:
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var duration = g_form.getValue('u_troubleshooting_duration');
var ajax = new GlideAjax('AjaxCostCalc');
ajax.addParam('sysparm_name', 'costCalc');
ajax.addParam('sysparm_dur', 'duration');
ajax.getXMLWait();
var answer = ajax.getAnswer();
alert(answer);
}
System Include Script:
var AjaxCostCalc = Class.create();
AjaxCostCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
costCalc: function() {
var dur = this.getParameter('sysparm_dur');
return current.dur.getGlideObject().getNumericValue();
}
});
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2016 02:19 PM
Hi Alfred,
I recommend doing this all on the client-side. No need for an extra round trip to the server.
Client script, assuming a duration field named u_duration
var dur = g_form.getValue("u_duration");
var parts = dur.split(" ");
var dayPart = (parts.length > 1) ? Number(parts[0]) : 0;
var hourPart = Number(parts[parts.length-1].split(":")[0])
alert("\ndays: " + dayPart + " \nhours: " + hourPart);
Here's a screenshot of how to test it using the Javascript executor (shft+ctrl+J on a Mac)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2020 10:46 AM
Hello - I realize this is a post from a few years ago, but I was wondering if you are able to tell me how to adjust the script you provided to grab only hours from a duration field, and not the days. I am trying to get Planned Effort and Actual Effort on a Project form, which only have hours/minutes/seconds available to manually enter. I only need the hours pulled from each field to calculate the percentage of how much actual effort has been done out of the planned hours, and display that in another field. If I use the script above, and I have 200 hours in the planned effort field, your script tells me that is 8 days and 8 hours. I need the raw value of 200.
Thanks in advance for your help!
Mike

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2020 05:13 AM
I figured out how ServiceNow is giving us the duration value, and with a few changes to your script you provided, I was able to get the value back into hours. I used this in a onSubmit client script in a project record:
function onSubmit() {
var effort = g_form.getValue('effort'); //planned effort
var parts = effort.split(" ");
var dayPart = (parts.length > 1) ? Number(parts[0]) : 0;
var hourPart = Number(parts[parts.length - 1].split(":")[0]);
var dayHours = dayPart * 24;
var totalEffort = dayHours + hourPart;
var actual = g_form.getValue('work_effort'); //actual effort
var parts2 = actual.split(" ");
var dayPart2 = (parts2.length > 1) ? Number(parts2[0]) : 0;
var hourPart2 = Number(parts2[parts2.length - 1].split(":")[0]);
var dayHours2 = dayPart2 * 24;
var totalActual = dayHours2 + hourPart2;
var percent = totalActual / totalEffort * 100;
g_form.setValue('u_hours_burned_percent', percent);
}
totalEffort and totalActual get the days and hours added up, and then I do a simple division of those values * 100 to get a percentage value that I put into Hours Burned Percent which is a decimal field type.
Thanks for the script you provided...that certainly helped me out immensely!
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2016 02:32 PM
You might also find the following Community thread helpful. It has a similar solution to reading a duration field on the client-side and converting the value to milliseconds: