Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to compare if current time falls within a time period,

Baba Femi
Tera Contributor

Hello community,

 

How can I script to check if current time falls within 9am and 5pm for example. The GDT uses the whole date time object, i'm only interested in time portion.

 

My use case is to route tickets based on ticket created time using assignment rule script. If created time is within 9am and 5pm, route to a specific team other to a default group.

 

Thanks

2 REPLIES 2

anshup
Tera Contributor

Were you able to get answer to this. If yes please past here in community as well.

Thanks in advance

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @Baba Femi 

Please tried this script:

(function() {

    var gdt = new GlideDateTime(current.sys_created_on);

    //CRITICAL: Set the Timezone to the team's location 
    // If you don't do this, it defaults to GMT/UTC or System time
    // Example: 'US/Pacific', 'Europe/London', 'Asia/Kolkata'
    var timeZone = 'US/Eastern'; 
    gdt.setTZ(Packages.java.util.TimeZone.getTimeZone(timeZone));


    var hour = gdt.getLocalTime().getHourOfDayLocalTime();

    //Logic: 9am (9) to 5pm (17)
    if (hour >= 9 && hour < 17) {
        current.assignment_group.setDisplayValue('Day Shift Team');
    } else {
        current.assignment_group.setDisplayValue('Night Shift Team');
    }

})();


Happy to help! If this resolved your issue, kindly mark it as the correct answer and Helpful and close the thread so others can benefit too.
Warm Regards,
Deepak Sharma
Community Rising Star 2025