The CreatorCon Call for Content is officially open! Get started here.

start date should be today + 2 business days

Thomas99
Tera Contributor

Posting this again as I could not get it working:

 

Hi

 

Can someone please help me with catalog client script (on change) and script include

 

I have a date variable in the new hire form.

The requirement is users should only be allowed to select the "start date" as current date + 2 business days (exclude sat-sun). 

 

Example: If I'm filling the form today , (Oct 11) the start date should be Oct 14 or further. User should not be be able to select an ealier date and should give an error " Invalid date" 

 

Thanks,

21 REPLIES 21

AnveshKumar M
Tera Sage
Tera Sage

Ignore this answer, I didn't see the Business Days, I'll post another one for Business Days case.

 

Hi @Thomas99 

 

Please try the following onChange client script, let me know if you still have any issues.

 

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

    return;

  }

 

  var milli_seconds_from_now = 48 * 60 * 60 * 1000;

  var date_obj = new Date(getDateFromFormat(newValue, g_user_date_format));

  var future_date = new Date(new Date().getTime() + milli_seconds_from_now);

  if(future_date <= date_obj){

      alert("The start date should be 2 days away from today");

gform.showFieldMsg("START_DATE_VARIABLE_NAME", "Date should be 2 days away from now", "error");      g_form.setValue("START_DATE_VARIABLE_NAME", ""); //Clear the selected date

  }

}

Thanks,
Anvesh

Community Alums
Not applicable

Hi @Thomas99 

try the code in On Change client script ,

 

(function () {
var currentDate = new GlideDate();

var targetStartDate = new GlideDate();
var businessDaysToAdd = 2; // Number of business days to add

while (businessDaysToAdd > 0) {
// Move the target date one day ahead
targetStartDate.addDays(1);

// Check if the target date is a business day (Monday to Friday)
if (isBusinessDay(targetStartDate)) {
businessDaysToAdd--;
}
}g_form.setValue('your_start_date_field', targetStartDate);

// Function to check if a given date is a business day (Monday to Friday)
function isBusinessDay(date) {
var dayOfWeek = new GlideDateTime(date).getDayOfWeek();
return dayOfWeek >= 2 && dayOfWeek <= 6;
}
})();

 

// Replace 'your_start_date_field' with the actual field name on your form that represents the start date.

//Please mark helpful if its worked.

Ankur Bawiskar
Tera Patron
Tera Patron

@Thomas99 

It's simple

1) use GlideAjax and pass the start date

2) add 2 business days to today's date

3) now compare start date and date received in step 2

4) if the start date is more than date from step 2 you are good else clear the variable and show field message

what did you start with and where are you stuck?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Thomas99 

 

1.Use Glide Ajax to call script include

2.In script include you write script like take  current date and add two days then get user selected date  from client script.

3. Then comapre date to weekend or not using if and else id condition

4.if user selected correct date means return true value else return false value

5.Here you must learn about GlideDateTime API for validations.

 

Thank you !