- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 02:09 AM
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;
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 02:50 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 02:35 AM
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
Thank you
G Ramana Murthy
ServiceNow Developer

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 02:50 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 03:28 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 03:49 AM
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).