- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2020 04:04 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2020 10:18 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2020 08:55 AM
Hi Satyach,
Thanks for the code.
Helped!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2020 10:32 PM
Hi,
Adding to all above, you have to convert value into duration .
Refer below script
var start = new GlideDateTime('start_date');
var start = new GlideDateTime('end_date');
var dur = GlideDateTime.subtract(start, end);
var duration = dur.getNumericValue();
var durationSeconds = (duration/1000);//like this convert seconds into hour and months etc.
gs.info(dur.getDisplayValue());gs.info(durationSeconds );
If my answer helped you in any way, mark answer as helpful and correct.
Thanks and regards,
Megha