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.

Check if current time is between 11pm today and 4am next day?

Nisar3
Giga Guru

I've a job that runs every 30 minutes. On each run, the first step I need to check is if the time that the job ran lies between 11pm to 4am.

 

In other words, I wish to have the job run everyday during off peak hours (11pm to 4am).

 

Is there an elegant way to make this validation?

1 ACCEPTED SOLUTION

Paul Kunze
Tera Guru

Here is your elegant way to do it 🙂

 

var glideDateTime = new GlideDateTime();
var currentHour = glideDateTime.getHourOfDayLocalTime(); // based on 24h
if (currentHour >= 4 && currentHour < 23) {
    return false; // do not execute Scheduled Job
}

 

View solution in original post

2 REPLIES 2

Paul Kunze
Tera Guru

Here is your elegant way to do it 🙂

 

var glideDateTime = new GlideDateTime();
var currentHour = glideDateTime.getHourOfDayLocalTime(); // based on 24h
if (currentHour >= 4 && currentHour < 23) {
    return false; // do not execute Scheduled Job
}

 

I had gone with this approach but since you answered it, will accept your solution.

 

var gt = new GlideTime();
var currentHour = gt.getHourOfDayLocalTime();
if([23,0,1,2,3].indexOf(currentHour) == -1){
	// outside of the required slot, so do nothing
	throw {message: 'Current hour is outside preferred slot' };
}

 

Btw, would there be any difference if I use GlideTime vs GlideDateTime as you've done it your example?