- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 04:31 AM
here is the business rule I've found in the community, for a field called u_days
u_setDays();
function u_setDays() {
var dte = gs.dateDiff(current.u_date_submitted.getDisplayValue(), current.u_date_accepted.getDisplayValue(); false);
current.u_days = dte;
}
It's calculating the number of days correctly, but I need it to show days only, not ddd hh:mm:ss
How do I set the display to show days only?
Any suggestions are much appreciated!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 06:16 AM
Hi Cheryl,
You can return the difference between the dates to a GlideDuration object, and get the number of days from GlideDuration using getDayPart().
u_setDays();
function u_setDays() {
var dte= GlideDateTime.subtract(new GlideDateTime(current.u_date_submitted), new GlideDateTime(current.u_date_accepted));
current.u_days = dte.getDayPart();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 05:13 AM
Hi Cheryl,
dateDiff returns a string, as you've noticed. You can use a string operator like split() to break out the days part. Something like this:
Warning: untested code ahead
u_setDays();
function u_setDays() {
var dte = gs.dateDiff(current.u_date_submitted.getDisplayValue(), current.u_date_accepted.getDisplayValue(); false);
var nDays = dte.split(' ')[0];
current.u_days = nDays;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 05:28 AM
Chuck,
In order to get an Integer result that is actually useful, should I be using another method?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 05:36 AM
If you want a real integer from that string, then use parseInt().
current.u_days = parseInt(nDays, 10);
FWIW, - parseInt() is a standard JavaScript method, not ServiceNow specific.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2017 05:36 AM
Hi Cheryl,
use parseInt() method to convert it into Integer and then store the value.
u_setDays();
function u_setDays() {
var dte = gs.dateDiff(current.u_date_submitted.getDisplayValue(), current.u_date_accepted.getDisplayValue(); false);
var nDays = dte.split(' ')[0];
current.u_days = parseInt(nDays);
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader