script was not working

Ak8977
Tera Expert

Hello all,
I have written a business rule for one case, in that script I have written the following logic but it was not working. The following is the scenario- 
there are two fields in utc_start_time and utc_end_time. I want the difference between the start time and endtime in  numeric value(minutes). because I need to multiply with some value after this logic. Can anyone helpme on this.

//utc_start and utc_end time are date/time field
var start_time = new GlideDuration(current.utc_start_time);
var end_time = new GlideDuration(current.utc_end_time);
var currentTime = new GlideDateTime();
var current_time = new GlideDuration(currentTime);

var myDuration;

if (current.u_utc_end_time != "") {
myDuration = end_time.subtract(start_time);
}
} else {
myDuration = current_time.subtract(start_time);
}

var minutes = myDuration.getNumericValue();

var res1 = minutes * current.count;

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

Here is an example to get the difference in minutes on two DateTime objects.

var startDate = new GlideDateTime(current.getValue('your_start_time_field'));
var endDate = new GlideDateTime(current.getValue('your_end_time_field'));

var differenceMS = endDate.getNumericValue() - startDate.getNumericValue();
var diffMinutes = differenceMS / (60 * 1000);

gs.info('diff Min: ' + diffMinutes);

View solution in original post

5 REPLIES 5

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Ak8977,

instead of myDuration = end_time.subtract(start_time);  this statement you can write like below

myDuration = GlideDateTime.subtract(end_time,start_time);

as well as instead of myDuration = current_time.subtract(start_time); this statement you can write like below

myDuration = GlideDateTime.subtract(current_time,start_time);

 

Please mark my answer helpful and correct, if it resolves ur query

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

OlaN
Giga Sage
Giga Sage

Hi,

Here is an example to get the difference in minutes on two DateTime objects.

var startDate = new GlideDateTime(current.getValue('your_start_time_field'));
var endDate = new GlideDateTime(current.getValue('your_end_time_field'));

var differenceMS = endDate.getNumericValue() - startDate.getNumericValue();
var diffMinutes = differenceMS / (60 * 1000);

gs.info('diff Min: ' + diffMinutes);

Hello @OlaN ,
the above logic working fine, but one thing is 
In the form start field value is 19/09/2023 15:04:00, if I  use the above logic and print the startDate it was showing 10 hours difference i.e 2023-09-19 05:04:00. why it was showing like this and how I can fix it.

The difference is because what is shown, is transformed into your timezone (your logged in user), which may be different from the timezone data that the system uses (usually GMT).