Calculate age based on date field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2017 12:58 PM
I am working on a custom app and would like to be able to calculate the age of something based on a date field on a form. I've found a few community posts that have some scripts that will probably get me most of the way there. Below is one example that I found for a Duration type field:
var gr=new GlideRecord('alm_hardware');
gr.query();
while(gr.next()){
gr.u_age=gs.dateDiff(gr.insall_date.getGlideObject().getDisplayValue(),gr.u_current_date.getGlideObject().getDisplayValue(), true);
gr.update()
}
Now, the question that I have currently is if it would be possible to get the value of the difference between a specific date field and today's date (based on the current system date) and then push that to a string field. That all seems easy enough, but wondering if there is also a way to determine the formatting of the field so that it would display the age in years and months like so: "6 Years, 4 months".
Not sure if anyone has had a requirement like this, but any help or direction is greatly appreciated. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2017 06:40 PM
I come across this requirement quite often. I haven't found an OOTB function that does it in the format I want. The closest I found is a function on the Duration object that will return something like "3 12:05:00".
This is the function I normally use:
function getDurationAsString(start, end) {
var seconds = gs.dateDiff(start.getDisplayValue(), end.getDisplayValue(), true); // returns seconds
var secondsInMIN = 60,
secondsInHOUR = 3600,
secondsInDAY = 86400,
secondsInMONTH = 2628000,
secondsInYEAR = 31535000,
units = [],
maxUnits = 2,
years = 0,
months = 0,
days = 0,
hours = 0,
mins = 0;
if (seconds >= secondsInYEAR) {
years = Math.floor(seconds / secondsInYEAR);
seconds -= years * secondsInYEAR;
units.push(years + ' Year' + (years == 1 ? '' : 's'));
}
if (seconds >= secondsInMONTH) {
months = Math.floor(seconds / secondsInMONTH);
seconds -= months * secondsInMONTH;
units.push(months + ' Month' + (months == 1 ? '' : 's'));
}
if (units.length < maxUnits && seconds >= secondsInDAY) {
days = Math.floor(seconds / secondsInDAY);
seconds -= days * secondsInDAY;
units.push(days + ' Day' + (days == 1 ? '' : 's'));
}
if (units.length < maxUnits && seconds >= secondsInHOUR) {
hours = Math.floor(seconds / secondsInHOUR);
seconds -= hours * secondsInHOUR;
units.push(hours + ' Hour' + (hours == 1 ? '' : 's'));
}
if (units.length < maxUnits && seconds >= secondsInMIN) {
mins = Math.floor(seconds / secondsInMIN);
seconds -= mins * secondsInMIN;
units.push(mins + ' Minute' + (mins == 1 ? '' : 's'));
}
if (units.length < maxUnits) {
units.push(seconds + ' Second' + (seconds == 1 ? '' : 's'));
}
return units.join(', ');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2017 02:23 AM
Hi Marcel,
As per your script
var gr=new GlideRecord('alm_hardware');
gr.query();
while(gr.next()){
gr.u_age=gs.dateDiff(gr.insall_date.getGlideObject().getDisplayValue(),gr.u_current_date.getGlideObject().getDisplayValue(), true);
gr.update()
}
what I understand is i table 'alm_hardware' u_age is a string field created. If not get it created named Age of type string. So, when script executes it sets the date in the Age field.
Changing the true paramater in the script below should help you something of format dd hh:mm:ss
gr.u_age=gs.dateDiff(gr.insall_date.getGlideObject().getDisplayValue(),gr.u_current_date.getGlideObject().getDisplayValue(), true);
to be replaced by
gr.u_age=gs.dateDiff(gr.insall_date.getGlideObject().getDisplayValue(),gr.u_current_date.getGlideObject().getDisplayValue(), false);