Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to convert duration field to a string value?

malaisamy1
Kilo Contributor

Hi Guys,

My problem is: I want to subtract two date/Time field and the result should stored in another field.

This i have achieved through this code using Business Rule.

"var gdt1 = new GlideDateTime(current.initial_response_date_time.getDisplayValue());  

var gdt2 = new GlideDateTime(current.open_date_time.getDisplayValue());  

var dur = GlideDateTime.subtract(gdt2, gdt1); //the difference between gdt1 and gdt2  

var dur1= new GlideDuration();  

dur1.setValue(dur.getValue());  

current.si_actual_response_duration=dur1.getDurationValue(); "

But the problem is i need to convert this duration field to seconds and this has to be stored in another field.

And also i have one more problem with this script is, the result gives me the duration along with date like this. "1970-01-01 01:00:00"

Can someone help me to resolve this issue?.

My Need is: Two date and time field has to be subtracted and the result should stored in another field as seconds.

Thanks in advance for the help.

Malaisamy J

1 ACCEPTED SOLUTION

sushantmalsure
Mega Sage

the result you need to store in another field if that is a date/time field then 1970-01-01 01:00:00 is a valid value to store 1 hour.


what's the type of field?


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

View solution in original post

11 REPLIES 11

sushantmalsure
Mega Sage

the result you need to store in another field if that is a date/time field then 1970-01-01 01:00:00 is a valid value to store 1 hour.


what's the type of field?


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

Thanks for the response.




Its a duration field.. But i want the result in seconds and that should be stored in string field. Is it possible?


here to set max unit



http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#Set_the_Maximum_Unit_of_...



try setting dictionary attribute of field to:


max_unit=seconds


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

Do i need to change the result field as string or it can be of duration field ?



Because i made the result field as string and checked its not working.



Help me on this.





Thanks,



Malaisamy