How Do I Get Integer or Numeric Values so I can do Math On Durations?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 12:33 PM
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-
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 12:46 PM - edited ‎07-16-2024 12:46 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 12:57 PM
Thank you. most of the way there, but still getting weird results on some of them, always with the 360...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 01:20 PM
Its whenever the hours is "09" which is apparently a radix issue, but even if I specify 10 it does not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 05:39 PM - edited ‎07-16-2024 05:43 PM
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