How to check if current time is between 8 AM to 10 AM

VIKAS MISHRA
Tera Contributor

I have one on before transform map script where i need to add one code such as if current time is 8 AM to 10 AM then the new tickets assignment group should be "A".

But i am not sure how to get the current IST time and then check if it is between 8 AM to 10 AM then perform some action.

Basically getting current IST time only not date and then then calculate if it is specific that time only difficult, please suggest.

4 REPLIES 4

DrewW
Mega Sage
Mega Sage

Simply doing 

var dt = new GlideDateTime();

gives you the current date/time in GMT.

You then need to use dt.getDisplayValue() to get it in the local time zone of the user running the code and then parse it to get the hour and check from there.

 

Your other option is to setup a schedule that has the time hours set for it.  Then just check to see if dt is in the schedule and go from there.

GlideSchedule | ServiceNow Developers

 

Using a schedule is probably the easiest way to deal with it.

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @VIKAS MISHRA 

Let try the GlideTime API to get the current hour of the day for your case.

getHourOfDayUTC(): Returns the hours part of the time using the UTC time zone. The number of hours is based on a 24 hour clock.

or

getHourOfDayLocalTime(): Returns the hours part of the time using the local time zone. The number of hours is based on a 24 hour clock.

 

So if your instance timezone is IST, try the getHourOfDayLocalTime. Otherwise, convert everything to UTC and use the getHourOfDayUTC one. (India Standard Time is 5:30 hours ahead of Coordinated Universal Time)

 

Cheers,

Tai Vu

Vrushali  Kolte
Mega Sage

Hello @VIKAS MISHRA ,

 

Below script should be helpful -

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    // Get the current date and time in UTC
    var currentDateTimeUTC = new GlideDateTime();

    // Convert the UTC time to IST (UTC +5:30)
    var currentDateTimeIST = new GlideDateTime(currentDateTimeUTC);
    currentDateTimeIST.addSeconds(19800); // 5 hours 30 minutes = 19800 seconds

    // Extract the hour part from the IST time
    var currentHourIST = currentDateTimeIST.getHourLocalTime();

    // Check if the current time is between 8 AM and 10 AM IST
    if (currentHourIST >= 8 && currentHourIST < 10) {
        // Assign the assignment group to "A"
        target.assignment_group = "A";
    }

})(source, map, log, target);

 

If my answer solves your issue, please mark it as Accepted✔️ & Helpful👍!

Akshay Gupta2
Kilo Sage

Hi @VIKAS MISHRA ,

 

Please try this script once - You can try this in Background script first.

I belive this can solve your issue. Later you can directly add this piece of code in your transform map directly.

** Note - you will get the current IST time with this script.

 

 

function getCurrentISTTime() {
        var gdt = new GlideDateTime();
        gdt.addSeconds(19800); // IST is UTC+5:30, which is 19800 seconds ahead of UTC
        return gdt.getTime();
    }

    // Get the current IST time
    var currentISTTime = getCurrentISTTime();

    // Convert the current IST time to hours and minutes
    var hours = currentISTTime.getHourLocalTime();
    var minutes = currentISTTime.getMinutesLocalTime();

    // Check if the current time is between 8 AM and 10 AM IST
    gs.print(currentISTTime)
    if ((hours === 8 && minutes >= 0) || (hours === 9) || (hours === 10 && minutes === 0)) {
        // Set the assignment group to "A"
        target.assignment_group = "A";
    }

 

  

Please let me know if this works for fou.

 

Thanks

Akshay Gupta