is there a way to format datediff to show days only

chennessy
Kilo Expert

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!

1 ACCEPTED SOLUTION

JennyHu
Tera Guru
Tera Guru

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();


}


View solution in original post

8 REPLIES 8

Chuck Tomasi
Tera Patron

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;


}


Chuck,


In order to get an Integer result that is actually useful, should I be using another method?


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.


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


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader