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

I actually did have the schedule part in my code, just not in the snippet I had pasted in here. However, I think the gem you have for me is the GetSeconds call, which will never have a leading zero, which is where my attempt was getting stuck every time there was "09" for hours... it would just give me NaN. Which is annoying, since we have a 09 hour work day, so it came up a lot. With seconds though, as you have in your example, I can convert it easily. Giving this a try!

So this is what I ended up with, the getSeconds did not work for me, it only returned zero all the time, but the GetNumericValue got me milliseconds and I can work with that. Waiting for this weekend to confirm the schedule works as desired, but I think it will.

 

var metric = new GlideRecord('metric_instance');
metric.addQuery("value",'Waiting For Employee Response');
metric.addQuery("definition",'c0a0d3ad1ba371d0a9e68775cc4bcb5d');
metric.addQuery("calculation_complete",'false');
metric.query();
while(metric.next()){
	var start= new GlideDateTime(metric.start).getDate();
	var today = new GlideDateTime().getDate();
	var schedule = new GlideSchedule(); 
	schedule.load('7e37105fa863010098fae0b06f83dc9d');//default schedule
	var dur = schedule.duration(start, today).getNumericValue();
	var hours = parseInt(dur) / 3600000;
	var days = hours / 9;
	gs.log("JAL: duration is: "+days +" "+metric.id);
	
	if((days>=4)){
		var hr = new GlideRecord('hr');
	    hr.addQuery('sys_id',metric.id);
	    hr.query();
		if(hr.next()){
		gs.eventQueue("hr.awaiting.response",hr);
	}
}}

 

For reference: see: DurationCalculatorAPI

 

Define a new schedule for your 9-hour days.

I think I am misunderstanding, as that is what we already have-

schedule.load('7e37105fa863010098fae0b06f83dc9d');//default schedule

 

Or is there something additional I am not considering? The above schedule is 8:30-5:30 PM EST.

The following is from the API documentation I posted a line to:

 

var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration("2019-01-02 11:00:00", "2019-01-06 09:00:00");
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();
var endDateTime = dur.getEndDateTime() + "";

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

make sure your script valiables: 'start' and 'today' are string values when calling 'calcScheduleDuration()'. Then do you math on the 'secs' variable.