using duration in update scripts doesn't produce the correct values.

mheyman
Tera Contributor

I have several locations where I am calculating a duration and then using that value later.   When I calculate the value based on the difference in two date time fields, it shows up correctly and displays correctly.  When I calculate based on what is showing in the duration field I get an entirely different value.   

 
Same script, different results depending on where I get the source data from.   
1st picture shows the table, with referenced table names.   
2nd picture shows 1 result of running the script.
 
 
u_laborstart = date/time
u_laborend = date/time
u_laborduration = duration
 
Calculating the duration as u_laborend - u_laborstart works correctly.
Pulling the duration from what should be the same value gives an entirely different value.  See code below.  
 
What am I doing incorrectly when I pull the value from the duration field that results in such wacky values?
 
var durr = new GlideDuration(fd_data.trigger.current.u_laborduration);
var sec = durr.getByFormat('s');
var min = durr.getByFormat('m');
var hour = durr.getByFormat('h');

gs.info("travel duration was: " + hour +":" + min + ":" + sec);

var lst = new GlideDateTime(fd_data.trigger.current.u_laborstart);
var lnd = new GlideDateTime(fd_data.trigger.current.u_laborend);
//gs.info("lstart was " + lst.getNumericValue());
//gs.info("lend was " + lnd.getNumericValue());
var diff = lnd.getNumericValue() - lst.getNumericValue();
//gs.info("diff was " + diff);

var retVal = new GlideDuration(diff);
//fd_data.trigger.current.u_laborduration = retVal
//fd_data.trigger.current.update();
//gs.info("retval is " + retVal.getDisplayValue());

var hours = retVal.getByFormat('HH');
//gs.info("hours: " + hours);
var min = retVal.getByFormat('m');
//gs.info("minutes: " + min);
var secs = retVal.getByFormat('s');
//gs.info("seconds = " + secs);
gs.info("time spent was " + hours + ":" + min + ":" + secs)


var LaborCost = 25;
var totalCost = (hours * LaborCost) + (min/60 * LaborCost) + (secs/3600 * LaborCost)

fd_data.flow_var.cost = totalCost;

//fd_data.trigger.table_name.u_laborcost = totalCost;
//fd_data.trigger.table_name.u_totallinecost = totalCost;
//fd_data.trigger.table_name.update();



return retVal.getDisplayValue();



 

 

1 ACCEPTED SOLUTION

kaushal_snow
Giga Sage

@mheyman ,

 

Duration field stores its value internally as a timestamp relative to the Unix epoch (e.g...... since 1970-01-01 00:00:00) rather than a simple raw difference, so if you try to build a GlideDuration from that stored value and then call getByFormat() you’ll often see unexpected results..... whereas calculating the difference between two GlideDateTime objects (e.g..... using GlideDateTime.subtract(start, end)) or using their numeric values gives you the correct duration value to work with in your script.........

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - Rising Star/Class of Legends 2025

View solution in original post

1 REPLY 1

kaushal_snow
Giga Sage

@mheyman ,

 

Duration field stores its value internally as a timestamp relative to the Unix epoch (e.g...... since 1970-01-01 00:00:00) rather than a simple raw difference, so if you try to build a GlideDuration from that stored value and then call getByFormat() you’ll often see unexpected results..... whereas calculating the difference between two GlideDateTime objects (e.g..... using GlideDateTime.subtract(start, end)) or using their numeric values gives you the correct duration value to work with in your script.........

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - Rising Star/Class of Legends 2025