- 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
04-02-2023 09:23 PM
where we used this script background or BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2020 05:01 AM
Hello J P Ramya,
You should add getDisplayValue() to the end of the sub variable.
Refer below code do changes accordingly:
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(g1);
gs.info(g2);
gs.info('Testing'+' '+sub.getDisplayValue());
}
I hope this will help you.
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.
Thanks,
Tejas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2020 05:28 AM
Hello Tejas,
The above code dint work for me,
Actually the output should be 1 year 14 months 1190 days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2020 05:59 AM
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.
Refer below code do changes accordingly:
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());
Output:
2 Days 23 Hours
You can try like this:
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 = GlideDateTime.subtract(g1,g2); //the difference between g1 and g2
var dur = sub.getDisplayValue();
gs.info('Testing'+' '+dur);
}
Output:
The output should be 1 year 14 months 1190 days.
For more details you refer below link:
Mark the answer as Correct/Helpful based on its impact.
Thanks & Regards,
Tejas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2020 11:52 AM
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.
Please refer below code do changes accordingly it will help you:
var startDate = new GlideDateTime("2011-08-28 09:00:00");
var endDate = new GlideDateTime("2013-01-21 08:00:00");
var startYear = startDate.getYear(); //get startYear year
var february = (startYear % 4 === 0 && startYear % 100 !== 0) || startYear % 400 === 0 ? 29 : 28;
var daysInMonth = [31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var yearDiff = endDate.getYear() - startYear; //the difference between startYear and endYear
var monthDiff = endDate.getMonth() - startDate.getMonth(); //the difference between startMonth and endMonth
if (monthDiff < 0) {
yearDiff--;
monthDiff += 12;
}
var dayDiff = endDate.getDayOfMonth() - startDate.getDayOfMonth(); //the difference between startDate and //endDate
if (dayDiff < 0) {
if (monthDiff > 0) {
monthDiff--;
} else {
yearDiff--;
monthDiff = 11;
}
dayDiff += daysInMonth[startDate.getMonth()];
}
gs.info(yearDiff + 'year' + ' ' + monthDiff + 'months' + ' ' + dayDiff + 'days');
Output: 1year 4months 23days
You can try like this:
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 = GlideDateTime.subtract(g1, g2); //the difference between g1 and g2
//gs.info('Testing'+' '+sub.getDisplayValue());
var startYear = g1.getYear(); //get startYear year
var february = (startYear % 4 === 0 && startYear % 100 !== 0) || startYear % 400 === 0 ? 29 : 28;
var daysInMonth = [31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var yearDiff = g2.getYear() - startYear; //the difference between startYear and endYear
var monthDiff = g2.getMonth() - g1.getMonth(); //the difference between startMonth and endMonth
if (monthDiff < 0) {
yearDiff--;
monthDiff += 12;
}
var dayDiff = g2.getDayOfMonth() - g1.getDayOfMonth(); //the difference between startDay and endDay
if (dayDiff < 0) {
if (monthDiff > 0) {
monthDiff--;
} else {
yearDiff--;
monthDiff = 11;
}
dayDiff += daysInMonth[g1.getMonth()];
}
gs.info(yearDiff + 'year' + ' ' + monthDiff + 'months' + ' ' + dayDiff + 'days');
}
I hope this will help you.
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.
Thanks,
Tejas