How to exclude weekends while calculating date in custom form in flow ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
try updating as this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) return;
// Expecting format like "YYYY-MM-DD" from ServiceNow date field
var parts = newValue.split('-');
if (parts.length !== 3) return;
var year = parseInt(parts[0], 10);
var month = parseInt(parts[1], 10) - 1; // JS months: 0–11
var day = parseInt(parts[2], 10);
// Create a date at local midnight using explicit Y/M/D (no time zone ambiguity for weekday)
var selectdate = new Date(year, month, day);
var weekday = selectdate.getDay(); // 0 = Sunday, 6 = Saturday
// Block Saturday (6) and Sunday (0)
if (weekday === 0 || weekday === 6) {
g_form.clearValue('date');
g_form.showFieldMsg('date', 'Due Date cannot be selected as it falls on a weekend.', 'error');
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
please provide Root cause why it did not work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
because Date.getDay() is evaluated in the browser’s local time zone, and “midnight” on a date in India can be a different calendar day (and thus a different weekday) in the US
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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.
