How to validate the day of the date

PRASHANTHI SATH
Tera Contributor

In change request form the planned start date and planned end date should fall on Saturday.

When user selects the option 'CAB Required', the system allows the planned start and end dates only when any day of the date from planned start to end should be Saturday. 

2 REPLIES 2

Samaksh Wani
Giga Sage
Giga Sage

Hello @PRASHANTHI SATH 

 

For this, we need 2 scripts

Client Script: OnChange to select the changed value

Script Include: To check if the date is a weekday or weekend.

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Show error when weekend is selected
   var ga = new GlideAjax("DaySat");
   ga.addParam("sysparm_name","isSaturday");
   ga.addParam("sysparm_date",newValue);
   var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
	if(answer == 'false') {
	      alert('Please Select a Date on Saturday');
              return false;
	}
    }
}

Script Include

var DaySat = Class.create();
DaySat.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isSaturday: function() {
		var selected_date = this.getParameter("sysparm_date");
		var d = new GlideDateTime(selected_date);
		//Monday -1 sunday -7
		if(d.getDayOfWeekLocalTime() == 6){
			return true;
		} else {
			return false;
		}
	},
    type: 'DaySat'
});

 

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

 

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
You can auto populate the end date using below logic.

var gdt = new GlideDateTime("2021-12-01 12:00:00");//Thursday
var daystoBeAdded=6-parseInt(gdt.getDayOfWeekLocalTime());
gdt.addDays(daystoBeAdded);
gs.info(gdt)
gs.info(gdt.getDayOfWeekLocalTime())//if 6 then it is saturday

 


Thanks and Regards,

Saurabh Gupta