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
}

1 ACCEPTED SOLUTION

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

View solution in original post

7 REPLIES 7

@VIKASM535239375 

handle the logic in server side using GlideAjax

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

yashkamde
Giga Sage

Hello @VIKASM535239375 ,

 

I would recommend use getUTCDay() instead of getDay(), since the value was parsed as UTC in the first place.

var selectdate = new Date(newValue);
var day = selectdate.getUTCDay();
if (day === 0 || day === 6) {
    g_form.clearValue('date');
    g_form.showFieldMsg('date', 'Due Date cannot be selected as Weekend', 'error');
}

 

If my response helped mark as helpful and accept the solution.

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