How to convert Duration field to date time field and calculate the diff on incident form

sony8
Tera Contributor

I have a requirement as below where need to check difference between two different type fields

field 1: duration field 

field 2 : duration field

field 3: data/time field

Need to calculate below equation:

field_1 = difference (field_2 -field_3)

I am not able to find solution since not understanding how to convert duration field(field 2) to date/time for getting duration of field 1 

I have written business rule for this.

    var gtime = new GlideDateTime(current.field_3); // date/time field 
    var dur = new GlideDateTime(current.field_2); // not getting how to convert to date/time
    var dur1 = GlideDateTime.subtract(gtime, dur); //the difference between gdt3 and gdt2
    current.field_1 =dur1;
    current.update();

 

please help me with above calculation. 

Thanks in Advance..

1 ACCEPTED SOLUTION

Hi,

share this

var gtime = new GlideDateTime(current.field_3).getNumericValue();
var dur = current.field_2.dateNumericValue(); // this should give milliseconds value
current.field_1.setDateNumericValue(dur-gtime);
current.update();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Yes i have used gs.info(); What is happening was getting the value like below 197096 days 40 hours 20min 00 secs So I think there is some garbage value coming .

Hi,

share this

var gtime = new GlideDateTime(current.field_3).getNumericValue();
var dur = current.field_2.dateNumericValue(); // this should give milliseconds value
current.field_1.setDateNumericValue(dur-gtime);
current.update();

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you @Ankur Bawiskar Sorry I did mistake with the script later realised the line which I made wrong.

Glad to know.

Please mark appropriate responses helpful as well.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader