Convert Milliseconds to business days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 03:22 AM
Hi Team - I have the below script which returns the duration for performing certain activities. This duration is in milliseconds or full days.
Example: If you convert milliseconds - 486000000 into hours you get 135 hours. If you divide 135 by 9, it gives 15. Final outcome must be 15 Days. Output can come to any given value and must be Minutes, hours, or days
Like 15 minutes, 1 Hour or can be 5 days 2 hours, 15 minutes
var grScCatItem = new GlideRecord('sc_cat_item');
grScCatItem.get('5d9c4b3c1b154c90e7d6202e6e4bcb1e')
var duration = grScCatItem.getValue('delivery_time');
gs.print(duration);
var gdt = new GlideDateTime(duration);
gs.print(gdt.getNumericValue());
var dur = new GlideDuration(gdt.getNumericValue());
var days = dur.getDisplayValue();
gs.print(days);
*** Script: 1970-01-06 15:00:00
*** Script: 486000000
*** Script: 5 Days 15 Hours
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 03:26 AM
So you are trying to say is : 9 hours means a complete day (a business day?)
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 03:28 AM
Yes, 9 hours means a business day.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 03:36 AM
Hi @SM16
You can use below code-
function millisecondsToDuration(millisecond){
var oneDayInMilliseconds = 9 * 60 * 60 * 1000;
var totalDays = Math.floor(milliseconds / oneDayInMilliseconds);
var remainingMilliseconds = milliseconds % oneDayInMilliseconds;
var hours = Math.floor(remainingMilliseconds / (60 * 60 * 1000));
remainingMilliseconds %= 60 * 60 * 1000;
var minutes = Math.floor(remainingMilliseconds / (60 * 1000));
remainingMilliseconds %= 60 * 1000;
var seconds = Math.floor(remainingMilliseconds / 1000);
var duration = {
days: totalDays,
hours: hours,
minutes: minutes,
seconds: seconds
};
return duration;
}
var milliseconds = 486000000;
var result = millisecondsToDuration(milliseconds);
gs.info(JSON.stringify(result));
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 03:43 AM
I would need it in the same format as shown below. Also if there are no hours or minutes, text should be displayed like in your example
"days":15,"hours":0,"minutes":0,"seconds":0
*** Script: 5 Days 15 Hours