Issues with Duration calculation in scoped app

davilu
Mega Sage

Hi Community,

I'm trying calculate the duration between two dates, but separate them out by Year, Month, and Date.   For example, if Start Date = Jan 1, 2016 and End Date = July 4, 2017, I want my Year field to show 1 year, my Month field to show 7 months, and my Day field to show 4 days.   Here's what my form looks like:

find_real_file.png

I've seen other posts about duration and can't seem to get it to work in our scoped application.   Here's what I've done with the script include:

Name: counttime

Client callable: true

Accessible from: All Application Scopes

var counttime = Class.create();

counttime.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

calcutime: function() {

  var start= this.getParameter('sysparm_strt');

  var end = this.getParameter('sysparm_end');

  var answer = gs.dateDiff(start, end, true);

  answer = answer/(60*60*24*365);

  return answer;

  },

      type: 'counttime'

});

And my client script on my table looks like this:

Name: Duration

Type: onChange

Field: Year

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

          return;

    }

  var strt = g_form.getValue('from');

  var end = g_form.getValue('to');

  var ajax = new GlideAjax('counttime');

  ajax.addParam('sysparm_name','calcutime');

  ajax.addParam('sysparm_strt',strt);

  ajax.addParam('sysparm_end',end);

  ajax.getXMLWait();

  g_form.setValue('year', answer);

}

When I enter in my From and To dates, no numbers appear in my Year field.   Any advice on how to fix this?

7 REPLIES 7

Hey gowrisankar and explorenow, I modified my script includes and client script based on another community post:



Script Includes


Client Callable = true


Accessible from all application scopes



var ClientDateTimeUtils   = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


getDateTimeDiff: function(){  


var firstDT = new GlideDateTime(this.getParameter('sysparm_fdt')); //First Date-Time Field  


var secondDT = new GlideDateTime(this.getParameter('sysparm_sdt')); // Second Date-Time Field  


var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day  


var diff = GlideDateTime.subtract(firstDT, secondDT).getDayPart();


var timediff = this._calcDateDiff(diffTYPE, diff);  


return timediff;  


},  




//Private function to calculate the date difference return result in second, minute, hour, day.  


_calcDateDiff: function(diffTYPE, seconds){  


var thisdiff;  


if (diffTYPE == "day"){thisdiff = seconds/86400;}  


else if (diffTYPE == "hour"){thisdiff = seconds/3600;}  


else if (diffTYPE == "minute"){thisdiff = seconds/60;}  


else if (diffTYPE == "second"){thisdiff = seconds;}  


else {thisdiff = seconds;}  


return thisdiff;  


},  


type: 'ClientDateTimeUtils'


});



Client script


Type onChange


Field name is Year



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}




var start = g_form.getValue('from');


var end = g_form.getValue('to');  


var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.  


alert(start);


alert(end);



var ajax = new GlideAjax('ClientDateTimeUtils');  


ajax.addParam('sysparm_name','getDateTimeDiff');  


ajax.addParam('sysparm_fdt', start);  


ajax.addParam('sysparm_sdt', end);  


ajax.addParam('sysparm_difftype', dttype);  


ajax.getXML(doSomething);  




function doSomething(response){  


var answer = response.responseXML.documentElement.getAttribute("answer");


alert(answer);


g_form.setValue('year', answer);


}        


}



explorenow, I tried to use the GlideDateTime and .getDayPart() in the script includes like you showed me in my other question, but doesn't seem to work.   I've also put in a couple alerts in the client script, but nothing is showing up.   Thanks.


Hi David,



Can we try something like this, comparing the user's DOB with 12/31/1959 date and then converting into Years, Months and Days. Lat's see if this helps.



Script Include:


var DisplayUserBirthDetails = Class.create();


DisplayUserBirthDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getUserDetails : function() {


var UserRecord = new GlideRecord('sn_hr_core_profile');


UserRecord.addQuery('user', this.getParameter('sysparm_sys_id'));


UserRecord.query();


if(UserRecord.next()){


var userBdaydt = new GlideDateTime();


userBdaydt.setDisplayValue(UserRecord.getValue('date_of_birth'));


var thdt = new GlideDateTime();


thdt.setDisplayValue(this.getParameter('sysparm_threshold_date'));


var duration = gs.dateDiff(thdt.getDisplayValue(), userBdaydt.getDisplayValue(), true);


if(duration > 0)


return duration;


}


}


},


type: 'DisplayUserBirthDetails'


});



Client script:


function onLoad() {


//Type appropriate comment here, and begin script below


var user_sys_id = g_user.userID;


var userDetails = new GlideAjax("DisplayUserBirthDetails");


userDetails.addParam("sysparm_name", "getUserDetails");


userDetails.addParam("sysparm_sys_id", user_sys_id);


userDetails.addParam("sysparm_threshold_date", '12/31/1959');


userDetails.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


var diff_date = serverResponse.responseXML.documentElement.getAttribute("answer");


if(diff_date != null){


var num_years = Math.trunc(parseInt(diff_date)/31556736);


var num_months = Math.trunc((parseInt(diff_date) % 31556736)/2592000);


var num_days = Math.ceil(((parseInt(diff_date) % 31556736) % 2592000)/86400);


alert('Years: ' + num_years + ',   ' + 'Months: ' + num_months + ', ' + 'Days :' + num_days);


}


}



Result:


Date 1: 12/31/1959


Date 2: 08/07/1987


Difference: 868402800 seconds


Approx Date Conversion: 27 years, 6 Months, 10 days


find_real_file.png


Hi David,



Did you get a chance to check ?