- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks 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
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
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 weeks ago
handle the logic in server side using GlideAjax
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 weeks 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
