How to set date/time field for weekdays only

Snow User5
Kilo Contributor

Hi All,

I have created one Date-Time field.

I want to throw an error message to user, when user selects date on either saturday/sunday.

User must select only working days(Mon-Fri)

How to achieve it ?

 

10 REPLIES 10

eumak
Tera Guru

Hello,

you can achieve the above requirement via two steps.

1) Onchange Client Script 

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

    var day = g_form.getValue('date');
    var ga = new GlideAjax("WorkingDay");
    ga.addParam("sysparm_name", "WorkDay");
    ga.addParam("sysparm_date", day);
    ga.getXML(Day);

    function Day(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            alert('Please select Week Days from Monday-Friday');
            g_form.clearValue('date', true);
        }
    }
}

 

2) Script Include

 

var WorkingDay = Class.create();
WorkingDay.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    WorkDay: function() {
		var selected_date = this.getParameter("sysparm_date");
		var d = new GlideDateTime(selected_date);
		var dayCount = d.getDayOfWeekLocalTime();
		gs.info("date count - "  + dayCount);
		if(dayCount == 5 || dayCount == 6){  // 5 - sat & 6 - sun
			return true;
		} else {
			return false;
		}
	},
    type: 'WorkingDay'
});

 

Hope above code will help you, Mark it as helpful or Correct.

Cheers..!
Happy learning 🙂
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

Just one correction in the Script Include.

 

if(dayCount == 6 || dayCount == 7){  // 6 - sat & 7 - sun

 

Hope this helps.

 

Thanks,

Raj

--Raj

Hi @Raj Esh ,

Thanks but I am not sure regarding the values, when I tried to fetch the logs after selecting Sat or Sunday from the field. It was giving me the value as 5 & 6. 

Mark it as helpful or Correct.

Cheers..!
Happy learning 🙂
Tushar

Mark it as helpful or correct, If Applicable


Cheers..!

Happy Learning:)

Tushar

Hi Tushar,

 

Maybe instance "start of week". For me, it worked with 

if(dayCount == 6 || dayCount == 7){  // 6 - sat & 7 - sun

 

Thank you for the information.

 

Thanks,

Raj

--Raj