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.

Calculate next date based on schedule

Sudeep
Tera Contributor

Hello,
I am trying to calculate the next date based on the scheduled selected.
For example: 

Date I have is of today and schedule I am using is repeat every 2nd Saturday at 2 pm
How can I calculate the date of that Saturday based on the today's date.

Thanks in advance!


 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Sudeep,

I would create a schedule that'll repeat on Saturday and then add date to that schedule to get the next date.

e.g.

Schedule. I've set the scheduled time to be 1 hour from 14:00 to 15:00.

find_real_file.png

Script

var startDate = new GlideDateTime();
startDate.setDisplayValue('2022-04-02 14:00:00');
var days = 1;
var dur = new GlideDuration(60 * 60 * 1 * 1000 * days);
var sched = new GlideSchedule('2896217597fa011086d3b4b3f153af7d');
var end = sched.add(startDate, dur);
gs.info(end.getDisplayValue());

View solution in original post

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Sudeep,

I would create a schedule that'll repeat on Saturday and then add date to that schedule to get the next date.

e.g.

Schedule. I've set the scheduled time to be 1 hour from 14:00 to 15:00.

find_real_file.png

Script

var startDate = new GlideDateTime();
startDate.setDisplayValue('2022-04-02 14:00:00');
var days = 1;
var dur = new GlideDuration(60 * 60 * 1 * 1000 * days);
var sched = new GlideSchedule('2896217597fa011086d3b4b3f153af7d');
var end = sched.add(startDate, dur);
gs.info(end.getDisplayValue());

My bad. It was to calculate the next date based on today. I've used Schedule in case it's necessary to consider holidays.

Comment out the startDate.setDisplayValue().

var startDate = new GlideDateTime();
var days = 1;
var dur = new GlideDuration(60 * 60 * 1 * 1000 * days);
var sched = new GlideSchedule('2896217597fa011086d3b4b3f153af7d');  // sys_id of "Every Saturday at 2pm" schedule
var end = sched.add(startDate, dur);
gs.info(end.getDisplayValue());

Execution result:

*** Script: 2022-04-16 14:00:00

Hi,

Can I create a business rule for this schedule?