Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Adding 30 Business days to a date field

Avatar

Level 2

I'm trying to add 30 business days to a date field. The client I initially had only gave me a +30 days so I just used:

FloatingDate = Num2Date(Date2Num($.formattedValue, "YYYY-MM-DD") + 30, "YYYY-MM-DD") however, they now need it in business days specifically. I've been looking into a possible solution using javascript, but it's honestly been a while since I've touched javascript and I'm a bit rusty when it comes to grabbing the date and adjusting the times. Any assistance would be greatly appreciated.

 

Thanks

1 Reply

Avatar

Level 10

Hi there,

 

I haven't checked out how to do this using FormCalc, but with JavaScript it is do-able.

One way to do it would be having a loop to iterate as many times it needs to end up adding 30 working days. This technically will not consider public holidays, if you want to take it in consideration you will need to add in some more details.

So here's how I'd do it:

 

 

//Verify that the value of the date field is different than null
if (initDate.rawValue != null){
	//Split the year/month/day of the date to help create a Date object
	var arrValues = initDate.rawValue.split("-"),
		//Call the date constructor using the values stored in an array (month are index based which requires a -1)
		startDate = new Date(arrValues[0], arrValues[1]-1, arrValues[2]),
		//The weekend "Day" values are 6 and 0 (saturday and sunday)
		arrWeekend = ["0", "6"],
		//Use initialDay if you want to count the selected date within the 30 working days
		initialDay = arrWeekend.indexOf(startDate.getDay().toString()) == -1 ? 1 : 0;
		
	//Loop until work days reach 30 											::: intWorkDays < 30
	//and if the 30th day ends in the weekend, skip to next working day			::: || (intWorkDays == 30 && arrWeekend.indexOf(startDate.getDay().toString()) > -1)
	for (var i = 0, intWorkDays = 0; intWorkDays < 30 || (intWorkDays == 30 && arrWeekend.indexOf(startDate.getDay().toString()) > -1); i=0){
	//Use the following loop statement if you wish to use initialDay
	////for (var i = 0, intWorkDays = initialDay; intWorkDays < 30 || (intWorkDays == 30 && arrWeekend.indexOf(startDate.getDay().toString()) > -1); i=0){
		//Verify that the current day is not a weekend day
		if (arrWeekend.indexOf(startDate.getDay().toString()) == -1){
			//Any working day will result in incrementing the working day variable
			intWorkDays +=1;
		}
		//For each iteration, increment the date by 1 day
		startDate.setDate(startDate.getDate() + 1);
	}
	//Print out the resulting value
	util.printd("yyyy-mm-dd", startDate);
}

 

 

You may want to verify the comments I've written to understand and alter the code to your liking.

I hope this will help, let me know.