Calculate the dates difference in days in Scoped Application

Swathi P
Tera Guru

Hi All,

 

we have a requirement to calculate the difference between two dates in days in scoped application.

I am trying to the difference in days in totalMs  but it is not giving proper value . Please help in the correcting the code

var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

validateDate1: function() {
var Date = new GlideDateTime();
var ActualEndDate = new GlideDateTime(this.getParameter('sysparm_date'));
var dur = GlideDateTime.subtract(Date, ActualEndDate);
var duration = dur.getNumericValue();
var totalMs = duration + 24*60*60*100; // to convert into days 
var durationDays = (duration/86400);
//gs.info("Test the Actual " + ActualEndDate);
//gs.info("Test the Actual Duration" + dur.getDisplayValue());
//gs.info(durationDays );


return totalMs;


},

type: 'DateValidation'
});

1 ACCEPTED SOLUTION

Lukasz Bojara
Kilo Sage

Hi,

Have you tried to use the getDayPart() method of GlideDuration that is a result of of GlideDateTime.subtract()? It will give you only the number of full days.

Also, the GlideDateTime.subtract() method has two parameters, ensure that the first parameter is not after the second, if first one will be after the second one you will have a different result. You can check if using the compareTo() method.

Have a look at this example:

var gdtNow = new GlideDateTime(gs.nowDateTime());
var gdtActual = new GlideDateTime('2019-11-01 18:00:00');
var compare = gdtNow.compareTo(gdtActual);
var days, difference;



if (compare === -1) {
  
  difference = GlideDateTime.subtract(gdtNow, gdtActual);
  days = difference.getDayPart();

} else if (compare === 1) {
  
  difference = GlideDateTime.subtract(gdtActual , gdtNow );
  days = difference.getDayPart();
  
} else {
	days = 0;
}

gs.print('Now: ' + gdtNow.getDisplayValue());
gs.print('Actual: ' + gdtActual.getDisplayValue());
gs.print('Difference: ' + difference.getDisplayValue());
gs.print('Days: ' + days);

 

If my answer helped you in any way, please then mark it as helpful. If this solved your case please mark it as a correct answer. Thank You.

 

Best regards,

Łukasz

 

View solution in original post

2 REPLIES 2

Lukasz Bojara
Kilo Sage

Hi,

Have you tried to use the getDayPart() method of GlideDuration that is a result of of GlideDateTime.subtract()? It will give you only the number of full days.

Also, the GlideDateTime.subtract() method has two parameters, ensure that the first parameter is not after the second, if first one will be after the second one you will have a different result. You can check if using the compareTo() method.

Have a look at this example:

var gdtNow = new GlideDateTime(gs.nowDateTime());
var gdtActual = new GlideDateTime('2019-11-01 18:00:00');
var compare = gdtNow.compareTo(gdtActual);
var days, difference;



if (compare === -1) {
  
  difference = GlideDateTime.subtract(gdtNow, gdtActual);
  days = difference.getDayPart();

} else if (compare === 1) {
  
  difference = GlideDateTime.subtract(gdtActual , gdtNow );
  days = difference.getDayPart();
  
} else {
	days = 0;
}

gs.print('Now: ' + gdtNow.getDisplayValue());
gs.print('Actual: ' + gdtActual.getDisplayValue());
gs.print('Difference: ' + difference.getDisplayValue());
gs.print('Days: ' + days);

 

If my answer helped you in any way, please then mark it as helpful. If this solved your case please mark it as a correct answer. Thank You.

 

Best regards,

Łukasz

 

James Gragston
Tera Guru
swathip,
 
What output are you looking for? Based on the function you're using, the output should be in this format:
'X Days X Hours'. There is another subtract() function that takes the argument in milliseconds and outputs the result like a normal GlideDateTime: '2011-12-07 07:59:59'. Is this the format you're looking for?
 
To achieve the latter format, you'll need to add some logic in your script to convert the '2 Days 23 Hours' string(if not a string, convert it to one) into milliseconds:
 
var time = "2 days 20 hours"; //OR in your case <var time = durationDays;>
gs.info(toMilliseconds(time));

function toMilliseconds(str) {
    var arr = str.split(' ');
    var days = arr[0];
    //convert string to number
    var daysInt = parseInt(days);
    gs.info(typeof daysInt);
    var hours = arr[2];
    //convert string to number
    var hoursInt = parseInt(hours);
    //convert days to hours and then total hours into milliseconds
    var milliseconds = (((daysInt * 24) + hoursInt) * 60 * 60) * 1000;
    return milliseconds;
}

The output of this function provides you with the difference in days and hours in a milliseconds conversion. You can use this output in the subtract() function.

 

Hope this answer helped or solved your problem. If so, please mark it as such. Thanks!

James Gragston | Developer at CloudPires


5721 Dragon Way | Suite 118
Cincinnati, OH 45227
CloudPires.com