Calculate Date in Business Days

Ken83
Mega Guru

Hello Community,

How can I calculate a target date counting business days only? 

Example : User enters start date on catalog item date field - 1/24/2022

Goal : The script should calculate 10 days from the start date considering *business days only* and should populate the end date field with 2/7/2022

 

8 REPLIES 8

Saurav11
Kilo Patron
Kilo Patron

Hello

Please use the below

var startDate = new GlideDateTime(current.start_date);

var days = parseInt(current.no_ofdays); //assuming there are 8 business hours

days = days*8;

var dur = new GlideDuration(60 * 60 * 1000 * days);

var schedule = new GlideSchedule('472acc8d1b521c1012c0dc6cdc4bcbb2'); //put your schedule sys_id here

var end = schedule.add(startDate, dur);

current.end_date=end;

Please mark answer correct/helpful based on impact

Hi Saurav,

I tried your script and unfortunately it did not provide the expected results

find_real_file.png

Can you share the script you executed?

var input = "01-24-2022";
var number = 10;

var startDate = new GlideDateTime(input);
gs.print(startDate);
var days = parseInt(number); //assuming there are 8 business hours

days = days*8;

var dur = new GlideDuration(60 * 60 * 1000 * days);

var schedule = new GlideSchedule(gs.getProperty('cmn_schedule.business_days')); //put your schedule sys_id here

var end = schedule.add(startDate, dur);

gs.print(end);​