- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2019 08:12 AM
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'
});
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2019 09:47 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2019 09:47 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2019 09:50 AM
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