- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2014 01:44 PM
Hi,
On the Requested Items table, we have created a Duration type field called 'Delivery time' which stores the difference between the Opened date and the Due Date. The purpose is to show the Promised Delivery time in Days for each RITM in a report.
While the field is able to give the difference, the customer wants the values to be rounded off to its nearest Day. For example, '2 Days 23 Hours 55 Minutes' should be rounded off to 3 Days.
Any help to make this happen is highly appreciated.
Regards,
Vishal
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2014 06:08 AM
Hi Nataraj,
You really do not want to use the "false" parameter as the final parameter in the dateDiff function. It performs an unnecessary conversion between integer and GlideDateTime. When you want to work with the integer value, you should use the true parameter. That said, your script does highlight a couple mistakes I made in my own (test, test, and test again ). Here is the corrected version which I have tested:
var answer = gs.dateDiff(current.opened_at, current.due_date, true); // Use dateDiff, not calDateDiff. calDateDiff is for use with legacy calendars.
answer = answer / 86400; // 86400 SECONDS in a day, this converts to days as a decimal. The dateDiff function returns the value in terms of seconds, not milliseconds.
answer = Math.round(answer); // Math.round rounds to the nearest whole day
answer = answer * 86400000; // Convert back to milliseconds, yes milliseconds because that is the natural integer value of GlideDates and GlideDateTimes
current.u_delivery_time = answer; // May need to convert to GlideDuration, not sure off hand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2014 08:42 PM
Hello Vishal,
GlideDurations are stored as the number of milliseconds. So we just have to convert to the number of days, round it, then convert back to milliseconds. Assuming you want to store it rounded, you could use the following in a business rule to calculate the duration:
var answer = gs.calDateDiff(current.opened_at, current.due_date, true);
answer = answer / 86400000; // 86400000 milliseconds in a day, this converts to days as a decimal.
answer = Math.round(answer); // Math.round rounds to the nearest whole day
answer = answer * 86400000; // Convert back to milliseconds
current.u_delivery_time = answer; // May need to convert to GlideDuration, not sure off hand
If you do need a GlideDuration instead of an integer, create a new GlideDuration and use its setNumericValue function.
Obviously you could do this with far fewer lines but I broke it up for explanation sake. To break up the 86400000: there are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day.
1000 * 60 * 60 * 24 = 86400000, the number of milliseconds per day.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2014 01:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2014 06:08 AM
Hi Nataraj,
You really do not want to use the "false" parameter as the final parameter in the dateDiff function. It performs an unnecessary conversion between integer and GlideDateTime. When you want to work with the integer value, you should use the true parameter. That said, your script does highlight a couple mistakes I made in my own (test, test, and test again ). Here is the corrected version which I have tested:
var answer = gs.dateDiff(current.opened_at, current.due_date, true); // Use dateDiff, not calDateDiff. calDateDiff is for use with legacy calendars.
answer = answer / 86400; // 86400 SECONDS in a day, this converts to days as a decimal. The dateDiff function returns the value in terms of seconds, not milliseconds.
answer = Math.round(answer); // Math.round rounds to the nearest whole day
answer = answer * 86400000; // Convert back to milliseconds, yes milliseconds because that is the natural integer value of GlideDates and GlideDateTimes
current.u_delivery_time = answer; // May need to convert to GlideDuration, not sure off hand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2014 12:56 PM
Thanks for your responses, Travis and Natraj.
We were able to get the duration values rounded off using the script provided by Travis.