Show duration field in only Days instead of Days hours minutes format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2019 01:43 AM
Hi
I am working on a feature for a product, I have to add a field in a table to calculate age in days. I created a dictionary entry of type duration named it age and marked it as function field. The function definition for the field is given as 'glidefunction:datediff(now(), last_opened);' the now() is a function which returns current date with time and last_opened is a field in the same table which is of type Date. This is calculating the age in ‘--Days --hours --minutes’ format but I need to show the date only in '--Days' format without hours and minutes. It is required to show this field in this format only. I cannot create a client script or other script to just trim the unnecessary part as this field will be used in the reports and dashboards for PA. So, I need to calculate this field in --Days itself.
Note:
- I intent to use only DB functions as other functions will be non performant. The table has millions of records so we cannot calculate manually the age for all records and set using update() or updateMultiple() and this cannot be market as calculated field. These will be non performant.
- type of the column cannot be changed to anything else otherwise the datediff function will return the duration in Date format. So, the column type should be such that the datediff will return in days format and not in the date format. example: if type is selected as integer/DateTime age_duration will be calculated as '1971-03-17 15:08:17' this is calculated as,
( 1970-01-01 00:00:00+<difference between now() and last_opened> ) this is how datediff function is defined but if we select duration field it is showing in --days --hours --minutes format.
this should be only '440Days' OR '440' OR rounded-off to '441' also works
Thanks
- 4,717 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2019 03:10 AM
Hi Digv,
Use GlideDuration getDayPart() function.
For example,
var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDayPart());
It gives output as 3 i.e. only number of days.
Also check below link.
https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_ScopedGlideDurationGetDayPart
I hope It will help you.
Regards,
Harshal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2019 07:54 AM
But the function getDayPart() does not work in the function definition it only works in the scripts. I already tried this but it did not work.
Thanks,
Digvijay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 10:00 AM
Hi Digv,
I know this is an old thread, but please let me know if you have ever found a solution to get only "days" format. We are facing the same issue as we recently upgraded to v12.0
Thanks in advance