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

Gowrisankar Sat
Tera Guru

      Hi,



dateDiff() method is not allowed in scoped applications. Instead you can use:



subtract(GlideDateTime start, GlideDateTime end)


Sample code:



var gdt1 = new GlideDateTime("2011-08-28 09:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");

var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());



If you want numeric value of the duration which is in milliseconds:


gs.info(dur..getNumericValue());



If the field is Date rather than Date Time, use:


var dur = GlideDate.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2



Sample Code:



var sgd1 = new GlideDate();


sgd1.setDisplayValue('2014-07-18');


var sgd2 = new GlideDate();


sgd2.setDisplayValue('2014-07-19');



duration= GlideDate.subtract(sgd1, sgd2);


gs.info(duration.getDisplayValue());


Thanks gowrisankar! I tried the subtract method, but it still doesn't work, any other ideas?


Gowrisankar Sat
Tera Guru

Hi David,



Can I know what value are getting using Subtract method?



You have to do this calculation:



parseInt(duration.getDisplayValue()/1000) = seconds


parseInt(duration.getDisplayValue()/1000*60) = minutes


parseInt(duration.getDisplayValue()/100*60*24 )= hours


parseInt(duration.getDisplayValue()/100*60*24*365) = years