logo

NJP

Select Weekdays in the date field

Import · Jan 14, 2020 · article

In this article, today i will share you a code snippet which will allow user to to select only a weekday from any of the date field in your form. If weekend is selected, we will show an error message.

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("selectWeekday");
   ga.addParam("sysparm_name","isWeekDay");
   ga.addParam("sysparm_date",newValue);
   var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
    if(answer == 'true') {
          g_form.showFieldMsg('expected_start','Select any weekday','error',true);
    }
    }
}

Script Include

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

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

View original source

https://www.servicenow.com/community/developer-articles/select-weekdays-in-the-date-field/ta-p/2323954