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

Ankita19
Tera Guru
Tera Guru

check this:

https://community.servicenow.com/community?id=community_question&sys_id=84515f53db77f3002be0a851ca961912

or try this:

var dur = current.u_duration.dateNumericValue();     //Get the duration value in millisecondsvar myDate = current.u_date.getGlideObject();     //Get the date/time field as a Glide Date Time objectmyDate.addSeconds(dur/1000);     //Add the seconds to the date

current.u_date = myDate;

sony8
Tera Contributor

Hi @Ankita  

i tried above script but it was converting into seconds but i want duration field conversion to date/time field , Do you have idea for the same?

Yousaf
Giga Sage

Hi Sony,

Can you please try 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 = gs.dateDiff(gtime, dur); //the difference between gdt3 and gdt2
    //current.field_1 =dur1;
    //current.update();
    gs.print(dur1);

Also refer to this link too

How to calculate difference between two dates in hours ?

 

Mark Correct or Helpful if it helps.

 


***Mark Correct or Helpful if it helps.***

sony8
Tera Contributor

Hi Yousif,

my requirement is there is two duration fields and one date time field.

Need to calculate duration 1

equation :  duration 1 = duration 2 - date/time 

i am thinking to convert duration 2 to Date/time field so that i can use below script for checking difference.

var duration1 = GlideDateTime.subtract(duration2(want to convert this field to date/time), date/time);

//if i convert like this i can get the calculation easy na thats why..

this is the expectation .Please help me if any approach