Solved: Calculate Number of Days between two dates(Start a... - ServiceNow Community

Calculate Number of Days between two dates(Start and End Date)

Dhanshri
Kilo Contributor

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.

 

1 ACCEPTED SOLUTION

Tejas Tamboli
Giga Guru

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

View solution in original post

8 REPLIES 8

Tejas Tamboli
Giga Guru

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

@Tejas Tamboli,

Thanks for your quick response.

It works as expected...

Great...

Glad to help you.

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