How to get difference between two dates(Start date and End date) in the scoped application?

J P Rohini
Tera Contributor

Hi all,

How to get difference between two dates(Start date and End date) in the scoped application?

i have tried this code, but dint work out,

var v1 = new GlideRecord('x_252275_video_video_table');
v1.query();
while(v1.next()){
var g1 = new GlideDateTime('start_date');
var g2 = new GlideDateTime('end_date');
var sub = new GlideDateTime.subtract(g1,g2);
gs.info('Testing'+' '+sub);
}

Output returns in this format:1970:11:21  8:00:54:06

But Output should be in this format: 1 year 11 months 12 days

Thanks in Advance

Any help is appreciated.

 

1 ACCEPTED SOLUTION

Satyanarayana C
ServiceNow Employee
ServiceNow Employee

Hi,

Please check if this works for you.

function convertDaysToYearsMonthsDays (diff) {
  var str = '';
  var values = [[' year', 365], [' month', 30], [' day', 1]];
  for (var i=0;i<values.length;i++) {
    var amount = Math.floor(diff / values[i][1]);
    if (amount >= 1) {
       str += amount + values[i][0] + (amount > 1 ? 's' : '') + ' ';
       diff -= amount * values[i][1];
    }
  }

  return str;
}


var dur = new GlideDuration();
dur.setValue('2018-03-01 07:30:00'); 

var duration2 = new GlideDuration();
duration2.setValue('2020-06-10 10:35:00'); 

var answer = duration2.subtract(dur);
gs.info(answer.getDisplayValue());

// get the day part
var gd = new GlideDuration(answer);
gs.info(convertDaysToYearsMonthsDays(gd.getDayPart()));

The function convertDaysToYearsMonthsDays - is taken from internet.

Thanks

View solution in original post

16 REPLIES 16

Hi @Tejas Tamboli  Its not working If End date value Less than Starting date value. Its showing Wrong date

 

 

Any update?

Is this answered?

If my answer helped you, kindly mark it as Correct & 👍Helpful so that others with the same question in the future can find it quickly & close the thread and that it gets removed from the Unanswered list.

 

Regards,

Tejas

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

I always give this example to people which helps you to get difference in days, min, sec,etc.

var v1 = new GlideRecord('x_252275_video_video_table');
v1.query();
while(v1.next()){
var firstDT = new GlideDateTime('start_date');
var secondDT = new GlideDateTime('end_date');
var diffTYPE = 'day';
var diff = gs.dateDiff(firstDT, secondDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
gs.print(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;
}

 

Thanks,
Ashutosh Munot

Function calDateDiff is not allowed in scope. Use GlideDateTime.subtract() instead and the other point is Function gs.print() is not allowed in scope. Use gs.debug() or gs.info() instead.

Satyanarayana C
ServiceNow Employee
ServiceNow Employee

Hi,

Please check if this works for you.

function convertDaysToYearsMonthsDays (diff) {
  var str = '';
  var values = [[' year', 365], [' month', 30], [' day', 1]];
  for (var i=0;i<values.length;i++) {
    var amount = Math.floor(diff / values[i][1]);
    if (amount >= 1) {
       str += amount + values[i][0] + (amount > 1 ? 's' : '') + ' ';
       diff -= amount * values[i][1];
    }
  }

  return str;
}


var dur = new GlideDuration();
dur.setValue('2018-03-01 07:30:00'); 

var duration2 = new GlideDuration();
duration2.setValue('2020-06-10 10:35:00'); 

var answer = duration2.subtract(dur);
gs.info(answer.getDisplayValue());

// get the day part
var gd = new GlideDuration(answer);
gs.info(convertDaysToYearsMonthsDays(gd.getDayPart()));

The function convertDaysToYearsMonthsDays - is taken from internet.

Thanks