Add 4 business days to date variable on a catalog item

Jeff316
Kilo Guru

Any solutions I could find on the internet have not worked.

I only get strange behavior and have to delete the variable and create again.

We have a date field on a catalog item.

The default date should be 4 business days in the future.

If anyone is currently doing this, please help.

We only use the portal UI.

Thanks.

 

1 ACCEPTED SOLUTION

did you try replacing

edt.getDate();

with

edt.getDisplayValue();

View solution in original post

19 REPLIES 19

Thanks for the quick reply Chuck.

I have to find a solution to add 4 business days to a date field.

I've tried the above script before when I didn't need the business days but the date comes out strange.

For example if I used that script today, it shows as 03-02-0170 in my date field

 

 

Chuck, 

FYI 

This is what I can use if I wanted to add 5 days to today's date as a default value in a date field.

Enter this string into the 'default' field of the Date catalog variable.

javascript:var d = new GlideDateTime(); d.setValue(gs.daysAgo(-5)); d.getDisplayValue();

I still have to figure out a script include/client script to do the same but to take into account the default date should appear as 5 business days in the future.

Why don't you simply add 7 days to your current date. Then it will basically set to 5 days past your current date. 

However, this logic works only when the selected date is a business day. If the selected date is on weekend, then you shoudl check if the date is sat, if sat, then add 6 days. If sun, then add 5 days. 

dvp
Mega Sage
Mega Sage

Try the below script

Update the schedule sys_id and number of hours on a business day

var gdt =  new GlideDateTime('2019-01-09');
	var dc = new DurationCalculator();
	dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Schedule sys_id
	 dc.setStartDateTime(gdt);
	  dc.calcDuration(4*9*3600); // 8-5 Schedule  so 9 Hrs
	
gs.log("Date: " + dc.getEndDateTime());

Hi DVP,

 This script also works. Gives me business days but won't take into account holidays where a schedule will.

// 4 business days
// first day of week MONDAY
var gdt = new GlideDateTime();
var days_to_add = 4
var day_of_the_week = gdt.getDayOfWeekUTC(); //monday will be 0
if(day_of_the_week==2){days_to_add = 7};
if(day_of_the_week>=3){days_to_add = 6};
gdt.addDaysLocalTime(days_to_add);

gs.info(gdt.getDate())

My problem now is, I don't know where or how to script these so they put the result into the Date Variable field on the catalog form.  Where would i use your suggested script to return the current date + # of business days based on schedule?