It is possible to restrict the date field to only allow weekdays to be chosen?

Kyle Pasmore1
Tera Contributor

I am trying to create a request that has a date field, but restrict this field to only allow users to select dates that are Monday - Friday. I need this to work with UK date formats (dd-mm-yyyy).

Here is what I have tried but it's not currently working, I think due to the format of the date?

Client script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	var ga = new GlideAjax('get_day_of_week');
	ga.addParam('sysparm_name', 'day_of_week');
	ga.addParam('sysparm_date' , newValue);
	ga.getXML(callbackFunction);
	
	function callbackFunction(response){
		var day = response.responseXML.documentElement.getAttribute('answer');
	
	alert(day);
	
	if (day == '6' || day == '7'){
		g_form.clearValue('delivery_date');
		g_form.showErrorBox('delivery_date' , "Please choose a weekday.");
	}
	}
}

Script include

var get_day_of_week = Class.create();
get_day_of_week.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	day_of_week : function(){
		var date = this.getParameter('sysparm_date');
		var gdt = new GlideDate(date);
		var today = gdt.getLocalDate();
		var day = gdt.getDayOfWeekUTC();
		//var day = gdt;
		return day;
		
	},

    type: 'get_day_of_week'
});

 

The above works when using the current date. ie var gdt = new GlideDate();

Is there a way to change the format of the delivery_date value to yyyy-mm-dd either client side/ server side and will this fix the issue? 

Or is there an alternate way to restrict this field?

1 ACCEPTED SOLUTION

Abhijit4
Mega Sage

I think getLocalDate and getDayOfWeekUTC method dont support for GlideDate Class.

Instead of using GlideDate, try with GlideDateTime. That should work.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

1 REPLY 1

Abhijit4
Mega Sage

I think getLocalDate and getDayOfWeekUTC method dont support for GlideDate Class.

Instead of using GlideDate, try with GlideDateTime. That should work.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP