- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 11:45 AM
Hello Geeks,
I have a field called as Purchased Date in asset table where in the date shows as something like 2021-02-12 10:30:00.
I have created a new integer field to calculate the age of the asset in years based on the current date - purchased date which gets updated using a schedule job on a monthly basis.
Below is the script
var gr = new GlideRecord('alm_hardware');
gr.addEncodedQuery('serial_number=BQP-854-D33246-GH');
gr.query();
while (gr.next())
{
var cur = new GlideDateTime();
gs.log('cur: '+cur);
var pur = new GlideDateTime(gr.install_date);
gs.log('pur: '+pur);
var diffSeconds = gs.dateDiff(pur.getDisplayValue(), cur.getDisplayValue(), true);
gs.log('Difference: '+diffSeconds);
var inYears=diffSeconds /(60*60*24*365);
gs.log('Years: '+inYears);
gr.u_device_age_years = inYears;
gr.update();
}
*** Script: cur: 2022-05-06 18:37:46
*** Script: pur: 2018-05-25 07:00:00
*** Script: Difference: 124630666
*** Script: Years: 3.9520124936580414
as you see from the output, When I update the record, it is getting updated as 4 instead of 3.
*** Script: Years: 3.3656024543378997, If the value is anything less that 3.5 then it is getting updated as 3.
I need to discard all the decimal values before I update the field.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 03:04 AM
I have used below code which worked fine.
var cur = new GlideDateTime();
var pur = new GlideDateTime(gr.u_purchase_date);
var dur = new GlideDuration(GlideDateTime.subtract(pur, cur));
var inYears = dur.getDayPart() / 365;
var Years = parseInt(inYears);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 12:00 PM
Hey,
Try below code:
var currentDate = new GlideDateTime("2022-01-18 16:48:13");
var openedAt = new GlideDateTime("2025-01-18 08:48:13");
var dur = new GlideDuration(GlideDateTime.subtract(currentDate, openedAt));
gs.info(dur.getDayPart());
gs.info(dur.getDayPart()/365);
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2022 09:35 AM
Hi,
Here's another option for you, there's really no need to convert the dates into seconds or days or something similar..
var today = new GlideDateTime();
var installDate = new GlideDateTime(gr.getValue('install_date'));
var counter = 0;
while (installDate.before(today))
{
installDate.addYearsUTC(1);
counter++;
}
gs.info('Difference in years: ' + counter);
Do note that my solutions totally disregards if the value is veeeeery close to the todays date, it still doesn't count.
Ex:
purchase date 2018-05-07 compare to 2022-05-08 -> 5
purchase date 2018-05-08 compare to 2022-05-08 -> 5
purchase date 2018-05-09 compare to 2022-05-08 -> 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2022 08:09 PM
Hi Aditya,
If it's only difference in years, use .getYearLocalTime() to get the year of GlideDateTime.
var purYear = pur.getYearLocalTime();
var curYear = cur.getYearLocalTime();
var inYears = curYear - purYear;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 03:04 AM
I have used below code which worked fine.
var cur = new GlideDateTime();
var pur = new GlideDateTime(gr.u_purchase_date);
var dur = new GlideDuration(GlideDateTime.subtract(pur, cur));
var inYears = dur.getDayPart() / 365;
var Years = parseInt(inYears);