How to set date/time field for weekdays only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 04:17 AM
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 ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 05:46 AM
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
Cheers..!
Happy Learning:)
Tushar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 12:19 PM
Just one correction in the Script Include.
if(dayCount == 6 || dayCount == 7){ // 6 - sat & 7 - sun
Hope this helps.
Thanks,
Raj

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2022 11:35 PM
Hi
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
Cheers..!
Happy Learning:)
Tushar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 02:05 AM
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