- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2021 05:58 AM
Hello Expert,
I want to calculate the number of days between two date fields. but this give me the less number of days between the two dates.
For example, the dates are 17-05-2021 to 18-05-2021, I am using the script below, the script is returning the value of '1', whereas it should be '2'. How can I get the correct value?
function setNumberOfDays() {
var start = current.u_start_date;
var end = current.u_end_date;
var d = gs.dateDiff(date1, date2, true);
current.u_total_number_of_days = d;
}
Thanks in advanced.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2021 06:02 AM
Hi Dhanshri,
You will want to convert the dates to the beginning and end of the days in some fashion.
So, Please refer to the below script code it will help you...
function setNumberOfDays() {
var start = new GlideDateTime('current.u_start_date');
var end = new GlideDateTime('current.u_end_date');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
current.u_total_number_of_days = days;
}
Else try like this in Background script:
var start = new GlideDateTime('2021-05-17 00:00:00');
var end = new GlideDateTime('2021-05-18 23:59:59');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
gs.print(start);
gs.print(end);
gs.print(days);
output:
*** Script: 2021-05-17 00:00:00
*** Script: 2017-05-18 23:59:59
*** Script: 2
For more details: GlideDateTime.subtract() and GlideDuration.getRoundedDayPart()
Mark the answer as ✅ Correct/Helpful based on its impact.
Thanks & Regards,
Tejas Tamboli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2021 06:02 AM
Hi Dhanshri,
You will want to convert the dates to the beginning and end of the days in some fashion.
So, Please refer to the below script code it will help you...
function setNumberOfDays() {
var start = new GlideDateTime('current.u_start_date');
var end = new GlideDateTime('current.u_end_date');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
current.u_total_number_of_days = days;
}
Else try like this in Background script:
var start = new GlideDateTime('2021-05-17 00:00:00');
var end = new GlideDateTime('2021-05-18 23:59:59');
var diff = GlideDateTime.subtract(start, end);
var days = diff.getRoundedDayPart();
gs.print(start);
gs.print(end);
gs.print(days);
output:
*** Script: 2021-05-17 00:00:00
*** Script: 2017-05-18 23:59:59
*** Script: 2
For more details: GlideDateTime.subtract() and GlideDuration.getRoundedDayPart()
Mark the answer as ✅ Correct/Helpful based on its impact.
Thanks & Regards,
Tejas Tamboli
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2021 06:57 AM
Thanks for your quick response.
It works as expected...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2021 07:12 AM
Great...
Glad to help you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2022 12:49 PM
Hello,
I am trying to do the same thing, but need to do a calculation after the number of days are calculated, so I was wondering if I needed to put this calculation into a field type first. If so, do I need to create an integer type field first, and then put this logic onto the calculation section of the integer field?
Thanks!
Annica