- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 11:20 PM
Hi Everyone,
I am running a background script on case table to get the opened date and closed date of the case. Then I am getting the difference between the two days ( Closed Date - Opened Date), I am getting something like this in script , 13 Days 20 Hours 22 Minutes . I want to convert this into hours in script like 332 Hours.
Can anyone help in converting days to hours.
Thanks in advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 11:35 PM
Hi @Abhijit Das7 here is the script
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 11:30 PM
Hi,
Could you please share your script?
Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 11:35 PM
Hi @Abhijit Das7 here is the script
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 11:55 PM
Tested below code on background script, working as expected:
// Assuming 'duration' is the variable containing the duration "13 Days 20 Hours 22 Minutes"
duration="13 Days 20 Hours 22 Minutes";
var durationParts = duration.split(" "); // Split the duration string into parts
var days = parseInt(durationParts[0]) || 0; // Extract days part and convert to integer
var hours = parseInt(durationParts[2]) || 0; // Extract hours part and convert to integer
var minutes = parseInt(durationParts[4]) || 0; // Extract minutes part and convert to integer
// Convert days and hours into total hours
var totalHours = (days * 24) + hours + Math.round(minutes / 60);
gs.info("Total hours: " + totalHours);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2024 12:00 AM
Hi @Abhijit Das7,
Check out this script
var gr = new GlideRecord('sn_customerservice_case');
gr.addQuery('sys_id', '00c7e29987498a1050fa0f29dabb35a8');
gr.query();
if (gr.next()) {
var openedDate = gr.getValue('sys_created_on');
gs.info("Opened Date: " + openedDate);
var closedDate = gr.getValue('closed_at');
gs.info("Closed Date: " + closedDate);
var gdt1 = new GlideDateTime(openedDate);
var gdt2 = new GlideDateTime(closedDate);
// Subtract gdt2 from gdt1 to get the duration
var dur = GlideDateTime.subtract(gdt1, gdt2);
gs.info("Duration: " + dur.getDisplayValue());
// Calculate duration in hours
var hours = dur.getNumericValue() / (1000 * 60 * 60); // Convert milliseconds to hours
gs.info("Duration in Hours: " + hours);
}
Here is a output
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!