How to check selected Date/Time Variable value is Only Weekdays and 8AM to 12 PM only?

HARI KISHAN GVS
Mega Sage

Hi Team,

I wanted to chek the selected Date/Time Variable value should only be on weekdays and between 8AM to 12 PM only.My thought would be create a onchange Client script on my date time variable called 'date_time_example' to check the selected value(newValue) is only a weekday and time should be between 8AM to 12 PM.

if it's not throw an alert and clear the variable Value.

Please can anyone provide me a script?

Thanks in advance.

Hari kishan.

3 REPLIES 3

Allen Andreas
Administrator
Administrator

Hello,

You can utilize GlideAjax in that onChange client script and pass the value from the variable field to a script include, then set the display value of a GlideDateTime object variable to that passed value, then you can check the day of the week, via API to ensure it's weekdays as well as check the time, to ensure it's between 8AM and 12PM (of a certain time zone?).

Then, pass true or false back to the client, which in turns either shows an alert...or not...

Please review the links above and attempt yourself.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

As you recommended it, I tried using Glideajax in my clientScript with no luck.

don't know if my script is correct. can you please correct my script.

Thaks in advance.

Hari Kishan.

//----------- Client Callable Script Include-----------------------
var ValidateDateFieldsUtil = Class.create();
ValidateDateFieldsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkDateEntered: function() {
        // Check date to validate it is in future and only on weekdays and between 8AM to 12PM
        var answer = "";
        var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
        var selectedDate = selectedDateTime.getDate();
        var selectedTime = selectedDateTime.getTime();
        var selectedDay = selectedDateTime.getDayOfWeek();
        var hours = selectedDateTime.toString();
        var dateSplt = hours.split(' ');
        var time = dateSplt[1];
		var nowDateTime = new GlideDateTime(gs.nowDateTime());
		var nowDate = nowDateTime.getDate();    
        if ((selectedDate < nowDate) || (selectedDate == null)) {
            if ((selectedDay == 1) || (selectedDay == 2) || (selectedDay == 3) || (selectedDay == 4) || (selectedDay == 5)) {
                if ((time <= 12) && (time >= 8)) {
                    answer = true;
                } else {
                    answer = false;
                }

            }
        }
        type: 'ValidateDateFieldsUtil';
    }
});


//------------------ On change Catalog ClientScript-----------------------------
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var ga = new GlideAjax('ValidateDateFieldsUtil');
	ga.addParam('sysparm_name','checkDateEntered');
	ga.addParam('sysparm_date', newValue);
	ga.getXML(ValidateDateResponse);
function ValidateDateResponse(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if(answer == "false"){
		alert('date should be in weekdays and time between 8AM and 12 PM');
		g_form.setValue('date_time_example', "");
}
}   
}

Hi,

Really nice attempt. Only thing I would say about it is to add some log statements, such as

gs.info("***DEBUG - the date is: " + selectedDate + " and the time is: " + selectedTime + " and the day of week is: " + selectedDay);

Add that to your script include so you can check values as needed. Repeat as necessary.

Additionally, another way is that you can use a schedule of weekdays and then 8-12 time and then check that value against that schedule using the GlideSchedule API.

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!