- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-08-2022 09:12 AM
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..
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-11-2022 07:55 AM
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
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2022 12:30 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2022 03:04 AM
Hi
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2022 03:13 AM
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.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-09-2022 03:18 AM
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