Configure function field to get date difference

pathumharsh
Tera Contributor

when I'm creating a report I had to get date difference between current date and table value. 

I used configure function field to do that, can anyone help me to do the calculation to get the value. 

u_access_last_verified is the table column

pathumharsh_0-1768387275223.png

 

7 REPLIES 7

@pathumharsh 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

4078_TA
Tera Guru

Hi @pathumharsh  ,

Your syntax should look like this:

glidefunction:datediff(now(), u_access_last_verified)

 

If you want the number to be positive (e.g., "10 days ago"), and the date in your table is in the past, you might need to swap them: glidefunction:datediff(u_access_last_verified, now())

SagnicDas_dev
Mega Guru

Hi @pathumharsh

(function() {

    var today = new GlideDateTime(); 
    
    var gr = new GlideRecord('sn_grc_indicator');
    gr.addEncodedQuery('item.sys_class_name=sn_risk_risk^ORitemISEMPTY^category=Risk Indicator^passed=failed^u_failed_dateISNOTEMPTY');
    gr.query();

    while (gr.next()) {
        gs.info('Processing GRC Indicator: ' + gr.number);

        var failedDate = new GlideDateTime(gr.u_failed_date); // Captured Indicator failed date
        var duration = GlideDateTime.subtract(failedDate, today);
        var diffInDays = duration.getDayPart();
        
        gs.info('The diffInDays is: ' + diffInDays);
        
    }
})();

@pathumharsh ,

I used a same kind of code in a Scheduled job to get the diffrence between start date and failed date in an Indicator.

So the code that I used is returning the day differnce between start and failed date. You may use the same code and modify it as per your use case.

(function() {

    var today = new GlideDateTime(); 
    
    var gr = new GlideRecord('sn_grc_indicator');
    gr.addEncodedQuery('item.sys_class_name=sn_risk_risk^ORitemISEMPTY^category=Risk Indicator^passed=failed^u_failed_dateISNOTEMPTY');
    gr.query();

    while (gr.next()) {
        gs.info('Processing GRC Indicator: ' + gr.number);

        var failedDate = new GlideDateTime(gr.u_failed_date); // captured the failed date from indicator
        var duration = GlideDateTime.subtract(failedDate, today);
        var diffInDays = duration.getDayPart();
        
        gs.info('The diffInDays is: ' + diffInDays);
        
    }
})();


Change the  " u_failed_date "  and put your end date field "u_access_last_verified" In place of it and you will get your desired Output.

If you find it helpful for you please mark it as helpful and accept the solution.

Regards,

Sagnic