Need help for one of the conversion

Are Kaveri
Tera Contributor

Hi Everyone,

 

I need help for one of the conversion.

 

we are sending data to third party system. we need to send the duration in days.

 

here we are subtracting auto sys fields as below.

var duration = updated - created,

we need duration in days.

 var start_time = new GlideDateTime(grTaskSla.getValue('start_time'));
                var end_time = new GlideDateTime(grTaskSla.getValue('end_time'));
                var duration = GlideDateTime.subtract(start_time, end_time);
                return duration.getDisplayValue();

 

 

Please help me how to fetch duration in days

3 REPLIES 3

Sohail Khilji
Kilo Patron
Kilo Patron

Use this :

 

var start_time = new GlideDateTime(grTaskSla.getValue('start_time'));
                var end_time = new GlideDateTime(grTaskSla.getValue('end_time'));
                var duration = GlideDateTime.subtract(start_time, end_time);
                var days = duration.getDayPart();
                   gs.info('days are :'+ days);

 

 

I hope it works for you...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi @Sohail Khilji ,

 

 

when i tried background script it was returning days.

when i tried in Business rule it was stopping my Business rule triggering.

Do you have any other suggestions.

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Are Kaveri 

To calculate the duration between two dates in days using GlideDateTime, you can use the getNumericValue() method to get the duration in milliseconds, then convert it to days. Here's how you can modify your code:

 

var start_time = new GlideDateTime(grTaskSla.getValue('start_time'));
var end_time = new GlideDateTime(grTaskSla.getValue('end_time'));
var durationInMillis = GlideDateTime.subtract(start_time, end_time).getNumericValue();
var durationInDays = durationInMillis / (1000 * 60 * 60 * 24); // Convert milliseconds to days
return durationInDays;

 



Please mark as helpfull & Accepted solution if you get it.

BR, Sohith

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)