Difference between dates into days is not working

Prudhvi Raj Y
Tera Expert

Hi,

I am trying to get the difference between two dates into days in a scoped application but somehow it's not working below is the code.

 

(function executeRule() {

    var gr = new GlideRecord('CUSTOM TABLE');
    gr.query();

    var rowCount = gr.getRowCount();
    var count = 0;
    while (gr.next()) {
        count++;

        var nowGdt = new GlideDateTime();
        var createdGdt = gr.sys_created_on;
        gs.info("get Dates: "+ nowGdt +" " + createdGdt);
        var diff = GlideDateTime.subtract(nowGdt, createdGdt);
        gs.info("get Diff: "+ diff);
        var dur = diff.getNumericValue();
        gs.info("getdiff: "+ dur);
        //var diffMs = nowGdt.getNumericValue() - createdGdt.getNumericValue();
        var elapsedDays = dur / (1000 * 60 * 60 * 24);

        var aging = '';
        if (elapsedDays <= 2) aging = '0-2 Days';
        else if (elapsedDays <= 7) aging = '3-7 Days';
        else if (elapsedDays <= 14) aging = '8-14 Days';
        else if (elapsedDays <= 21) aging = '15-21 Days';
        else if (elapsedDays <= 28) aging = '22-28 Days';
        else aging = '> 28 Days';
        if (aging) {
            gs.info('[AGE] ' + gr.number + ' → ' + aging);
        } else {
            gs.info('[WARN] ' + gr.number + ' → ' + 'NO ageing value assigned elasped days:' + elapsedDays);
        }
        gr.setWorkflow(false);
        gr.autoSysFields(false);
        gr.ageing_category = aging;
        gr.update();
    }
})();
 
Please let me know if something is missing in code

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Prudhvi Raj Y 

some corrections

try this

(function executeRule() {

    var gr = new GlideRecord('CUSTOM TABLE');
    gr.query();

    while (gr.next()) {

        var nowGdt = new GlideDateTime();
        // Create GlideDateTime object from sys_created_on string
        var createdGdt = new GlideDateTime(gr.sys_created_on.toString());

        // Subtract createdGdt from nowGdt to get GlideDuration
        var diff = GlideDateTime.subtract(createdGdt, nowGdt);

        gs.info("get Diff: " + diff);

        // diff is a GlideDuration; get numeric value in milliseconds
        var dur = diff.getNumericValue();

        gs.info("getdiff (ms): " + dur);

        // Convert milliseconds to days
        var elapsedDays = dur / (1000 * 60 * 60 * 24);

        gs.info("Elapsed days: " + elapsedDays);

        var aging = '';
        if (elapsedDays <= 2)
            aging = '0-2 Days';
        else if (elapsedDays <= 7)
            aging = '3-7 Days';
        else if (elapsedDays <= 14)
            aging = '8-14 Days';
        else if (elapsedDays <= 21)
            aging = '15-21 Days';
        else if (elapsedDays <= 28)
            aging = '22-28 Days';
        else
            aging = '> 28 Days';

        gs.info('[AGE] ' + gr.getValue('number') + ' → ' + aging);

        gr.setWorkflow(false);
        gr.autoSysFields(false);
        gr.ageing_category = aging;
        gr.update();
    }

})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Prudhvi Raj Y 

some corrections

try this

(function executeRule() {

    var gr = new GlideRecord('CUSTOM TABLE');
    gr.query();

    while (gr.next()) {

        var nowGdt = new GlideDateTime();
        // Create GlideDateTime object from sys_created_on string
        var createdGdt = new GlideDateTime(gr.sys_created_on.toString());

        // Subtract createdGdt from nowGdt to get GlideDuration
        var diff = GlideDateTime.subtract(createdGdt, nowGdt);

        gs.info("get Diff: " + diff);

        // diff is a GlideDuration; get numeric value in milliseconds
        var dur = diff.getNumericValue();

        gs.info("getdiff (ms): " + dur);

        // Convert milliseconds to days
        var elapsedDays = dur / (1000 * 60 * 60 * 24);

        gs.info("Elapsed days: " + elapsedDays);

        var aging = '';
        if (elapsedDays <= 2)
            aging = '0-2 Days';
        else if (elapsedDays <= 7)
            aging = '3-7 Days';
        else if (elapsedDays <= 14)
            aging = '8-14 Days';
        else if (elapsedDays <= 21)
            aging = '15-21 Days';
        else if (elapsedDays <= 28)
            aging = '22-28 Days';
        else
            aging = '> 28 Days';

        gs.info('[AGE] ' + gr.getValue('number') + ' → ' + aging);

        gr.setWorkflow(false);
        gr.autoSysFields(false);
        gr.ageing_category = aging;
        gr.update();
    }

})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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