Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Schedule Report three times a day

BibekP
Tera Contributor

Hello Team,

I'm trying to schedule an email to be sent three times a day at specific times: 08:00, 22:00, and 02:00. To do this, I selected "Run Periodically" with an interval of 1 hour, and used the following script in the "Conditional" field:

 

var gdt = new GlideDateTime();
var currentTime = gdt.getLocalTime().getByFormat('HH:mm'); // Get current time in HH:mm format

// Define the specific times
var time1 = '08:00';
var time2 = '22:00';
var time3 = '02:00';

if (time1 == currentTime || time2 == currentTime || time3 == currentTime) {
answer = true;
} else {
answer = false;
}

 

However, the email is being sent every hour instead of just at the three specified times.

Could anyone help me identify what I might be doing wrong? 

Thanks in advance!

2 REPLIES 2

Edxavier Robert
Mega Sage

Hi, 

You're on the right track, but the issue lies in how getLocalTime().getByFormat('HH:mm') behaves—it does not round or truncate seconds and milliseconds, and it may not exactly match '08:00', '22:00', or '02:00'.

 

Also, since the script runs every hour (e.g., at 08:00:00, 09:00:00, etc.), you want to ensure that you're comparing the hour and minute precisely, without worrying about seconds or milliseconds

You can try this:

 

var gdt = new GlideDateTime();

var hour = gdt.getHourLocalTime(); // returns hour (0–23)

var minute = gdt.getMinuteLocalTime(); // returns minute (0–59)

gs.info("Scheduled job check - Current local time: " + hour + ":" + minute);

if (minute == 0 && (hour == 2 || hour == 8 || hour == 22)) {

    gs.info("Scheduled job condition met - email will be sent.");

    answer = true;

} else {

    gs.info("Scheduled job condition NOT met - no email sent.");

    answer = false;

}

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@BibekP 

try this

var answer = false;

// Get current time in HH:mm format
var currentTime = getCurrentTime();

// Define the specific times
var times = ['08:00', '22:00', '02:00'];

// Check if the current time matches any of the specified times
if (times.indexOf(currentTime) !== -1) {
    answer = true;
} else {
    answer = false;
}

function getCurrentTime() {
    var gdt = new GlideDateTime();
    var currentTime = gdt.getDisplayValue().substring(11, 16); // Get current time in HH:mm format
    return currentTime;
}

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