How Do I Get Integer or Numeric Values so I can do Math On Durations?

jlaps
Kilo Sage

I have tried getting numeric value and a few other things, but I am not able to add two numbers together, let alone then divide by business hours etc. Duration using schedule is glomming 9 business hour days into 24 hour days, and I need to add it all together so I can then divide to get the actual business days, and am getting NaN or as shown below. What step am I missing?

 

var dur = schedule.duration(start, today);
	
	var bushours = dur.getDayPart() * 24;
	var busparthours = dur.getByFormat('HH');
	var tothours = bushours + busparthours;
	gs.log("JAL: duration is: "+bushours +"+" +busparthours +"=" +tothours);

This ends up looking like-

jlaps_0-1721158354982.png

 

The hours + partial hours are the correct numerals, but adding them together is not adding them as integers but as... strings or whatever. What am I missing in order to do some math here?

9 REPLIES 9

Bert_c1
Kilo Patron

Use javascript's parseInt() method on both of your two variables, then convert days to hours (days * 24) and add the two new variables to get total hours.

Thank you. most of the way there, but still getting weird results on some of them, always with the 360...

jlaps_0-1721159832736.png

 

Its whenever the hours is "09" which is apparently a radix issue, but even if I specify 10 it does not work. 

Hi jlaps,

 

Seems your script is not complete, and you want a Schedule applied.  To test using a schedule (8-5 Weekdays) of the time between two dates, I have the following.

 

 

var start = new GlideDateTime("2024-07-11 12:00:00");	// for testing
var today = new GlideDateTime();						// current date, for testing
gs.info("JAL: start = " + start + ", today = " + today);
// var dur = schedule.duration(start, today);
var dur = new DurationCalculator();
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');	// 8-5 weekdays, change to desired schedule
dur.calcScheduleDuration(start, today);
var secs = dur.getSeconds();
gs.info("JAL: secs = " + secs);

// get days and hours
var dayseconds = 60 * 60 * 24;
var hourseconds = 60 * 60;
var hours = (secs % dayseconds);

var days = (secs - hours) / dayseconds;
gs.info("JAL: secs = " + secs + ", days = " + days + ", hours = " + hours/hourseconds);
// Now the following logic may work using the above
//var bushours = dur.getDayPart() * 24;
//var busparthours = dur.getByFormat('HH');
var bushours = days * 24;
var busparthours = hours/hourseconds;

gs.info("JAL: bushours: "+bushours + ", busparthours: " + busparthours);

// calculate total hours
var tothours = bushours + busparthours;
gs.log("JAL: duration is: "+bushours +"+" +busparthours +"=" +tothours);

 

 

which results in:

 

 

** Script: JAL: start = 2024-07-11 12:00:00, today = 2024-07-17 00:39:23
*** Script: JAL: secs = 129600
*** Script: JAL: secs = 129600, days = 1, hours = 12
*** Script: JAL: bushours: 24, busparthours: 12
*** Script: JAL: duration is: 24+12=36

 

I hope this helps