Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

How to exclude weekends while calculating date in custom form in flow ?

VIKASM535239375
Kilo Sage

I have used below Client Script to restrict Selection of weekends in Custom Form.

 

It works fine for Indian Users and impersonating US Users in India. 

But when same US User tests in US it restricts Monday and not Saturday Why?

 

var selectdate = new Date(newValue);
var day = selectdate.getDay();
if (day === 0 || day === 6) {
g_form.clearValue('date');
g_form.showFieldMsg('Due Date cannot be selected as Weekend');//0 and 6 Saturday and Sunday
}

5 REPLIES 5

Ankur Bawiskar
Tera Patron

@VIKASM535239375 

try this, another logic

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || !newValue) return;

  // newValue from a date field is usually "YYYY-MM-DD"
  var parts = newValue.split('-');
  if (parts.length != 3) return;

  var year  = parseInt(parts[0], 10);
  var month = parseInt(parts[1], 10) - 1; // JS: 0–11
  var day   = parseInt(parts[2], 10);

  var d = new Date(year, month, day);
  var weekday = d.getDay(); // 0 = Sun, 6 = Sat

  if (weekday == 0 || weekday == 6) {
    g_form.clearValue('date'); // replace 'date' with your field name
    g_form.showFieldMsg('date', 'Due Date cannot be a weekend day.', 'error');
  }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader