Getting wrong Duration days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2012 07:51 AM
Hi,
I am trying to create a business rule to get the duration between two fields. I am using below script to get the duration:
*******************************************************************
stts = current.u_status;
if (stts =='Published')
{
var strt = current.u_tariff_effective_date;
var end = current.u_start_time;
var diff = calcDurationSchedule(strt,end);
gs.addInfoMessage(diff);
current.u_duration = diff;
}
function calcDurationSchedule(strt, end) {
var realStart = strt.getGlideObject();
var realEnd = end.getGlideObject();
gs.addInfoMessage(realStart);
gs.addInfoMessage(realEnd);
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', 'Weekdays 24 / 7');
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
dur = sched.duration(realStart,realEnd);
return dur;
}
*************************************************************************
Problem : When I make changes to the form, this Business rule get triggered. But I am getting wrong business duration. I have added 3 gs.addInfoMessage to get start date, end date and duration returned. I am getting correct date for Start date (2012-02-08) and End Date (2012-03-19) . But Duration returned is a date "1970-01-01 00:00:00". I am not sure why it returning date instead of number of days. Am I missing anything?
Thanks in advance.
ND

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2012 09:24 AM
Well, if you print a Duration as if it is just a string then you'll get something that looks like a date (it's a quirk of the system, a Duration is a special case of our DateTime and is stored as a date but treated as a duration.)
Use:
// human friendly form, can omit seconds when > 60s
gs.addInfoMessage(diff.getDisplayValue());
// another human friendly form, DDD HH:MM:SS
gs.addInfoMessage(diff.getDurationValue());
// raw seconds (getNumericValue() returns milliseconds)
var seconds = diff.getNumericValue() / 1000;
And if you want to derive the number of "days", take the seconds and then divide by the number of seconds in your "day" (which, for a non-24hr schedule won't be 86400). We don't currently have a convenience method that gives you the number of "business days".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2012 09:43 AM
Hi James,
Thanks for reply.
I have added below lines to code:
var seconds = diff.getNumericValue() / 1000;// numeric value is in milliseconds.
gs.addInfoMessage(seconds);
But it returns seconds as 0.0
Not sure why it is returning this weird value. Anyother way to do same thing?
ND

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2012 10:04 AM
"0.0" is definitely a little odd.
But I assume you're not expecting it to be zero either. What are your start and end date values that you're passing to your calcDurationSchedule() function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2012 10:20 AM