Validating the From_date and To_date and exclude the Saturday's and Sunday's using Client Development. (Client Scripts) in Servicenow
This article is about removing the weekdays from the calendar and calculate the days from the start date(i.e, From_date && To_date )
I tried this at Client side and it is working :
On change client script for from_date validation :
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return;
}
//Type appropriate comment here, and begin script below var from_date= new Date(g_form.getValue('from_date')); var date=new Date(); if(from_date<=date){ alert("enter valid date"); g_form.clearValue('from_date'); } var to_date=new Date(g_form.getValue('to_date')); if(to_date!=''){ if(to_date<=from_date) { alert("enter valid date"); g_form.clearValue('from_date'); } }
}
On change client script for To_date validation :
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return;
}
//Type appropriate comment here, and begin script below var from_date= new Date(g_form.getValue('from_date')); var date=new Date(); var to_date; if(from_date!=''){ to_date=new Date(g_form.getValue('to_date')); if(to_date<=from_date) { alert("enter valid date"); g_form.clearValue('to_date');
}
}
}
After these two Scripts, I have created another client side script for calculating the days between the from_date and to_date by removing the weekdays
Name : calculating the days excluding weekdays
Type of script : On change
Field : To_date
Script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return;
}
//Type appropriate comment here, and begin script below var gdt1=new Date(g_form.getValue('from_date')); var gdt2= new Date(newValue); alert(gdt1);
alert(gdt2);
var dayCount = 0; while (gdt1 <= gdt2) { gdt1.setDate(gdt1.getDate() + 1); if (gdt1.getDay() > 0 && gdt1.getDay() < 6) { dayCount = dayCount + 1; } } alert(dayCount);
g_form.setValue('no_of_leaves', dayCount);
}
I hope this will be helpful.
Kind Regards,
Sandhya morla
Labels:
https://www.servicenow.com/community/developer-articles/validating-the-from-date-and-to-date-and-exclude-the-saturday-s/ta-p/2330092
