Date field validation

Nipan
Tera Contributor

Hi, 

User can Select 5 days from today excluding weekend (Saturday, Sunday). 

 

I have written a UI policy, which is working for - "Start date should be 5 days from today in future"

Nipan_0-1743590684257.pngNipan_1-1743590715833.png

 

The above script is working correctly but it's including (counting) weekend also (Saturday, Sunday). 

 

Need help to exclude weekend and count 5 days (weekdays). 

 

Thanks,

Nipan

1 ACCEPTED SOLUTION

SriharshaYe
Kilo Sage

Hi @Nipan ,

We can achieve this using UI Policy Script  or a Client Script that automatically updates a field with the calculated number of weekdays excluding weekends i.e saturday and sunday

  • Using  UI Policy Script 

SriharshaYe_0-1743683928403.png

 

write the script as below : 

SriharshaYe_3-1743684207710.png

 

function onCondition() {
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
// convert start and end date to objets
var start=new Date(startDate);
var end = new Date(endDate);
var weekdaysCount = 0;
// Ensure the end date is not before the start date
if (start > end) {
return;
}
for (var currentDate = new Date(start); currentDate <= end; currentDate.setDate(currentDate.getDate() + 1)) {
var dayOfWeek = currentDate.getDay(); // Get the day of the week (0 = Sunday, 6 = Saturday)
// Check if it's a weekday (Monday to Friday)
if (dayOfWeek >= 1 && dayOfWeek <= 5) {
weekdaysCount++;
}
}
g_form.setValue('u_weekdays', weekdaysCount);
}

 

The above script calculates the weekdays , excluding weekends.

 

SriharshaYe_2-1743684159189.png

 

  • Using Client Script. 
  • create a new onchange client script as below  
  • SriharshaYe_4-1743684331382.png
  •  The above Script calculates the weekdays and excludes the weekends

 

View solution in original post

1 REPLY 1

SriharshaYe
Kilo Sage

Hi @Nipan ,

We can achieve this using UI Policy Script  or a Client Script that automatically updates a field with the calculated number of weekdays excluding weekends i.e saturday and sunday

  • Using  UI Policy Script 

SriharshaYe_0-1743683928403.png

 

write the script as below : 

SriharshaYe_3-1743684207710.png

 

function onCondition() {
var startDate = g_form.getValue('u_start_date');
var endDate = g_form.getValue('u_end_date');
// convert start and end date to objets
var start=new Date(startDate);
var end = new Date(endDate);
var weekdaysCount = 0;
// Ensure the end date is not before the start date
if (start > end) {
return;
}
for (var currentDate = new Date(start); currentDate <= end; currentDate.setDate(currentDate.getDate() + 1)) {
var dayOfWeek = currentDate.getDay(); // Get the day of the week (0 = Sunday, 6 = Saturday)
// Check if it's a weekday (Monday to Friday)
if (dayOfWeek >= 1 && dayOfWeek <= 5) {
weekdaysCount++;
}
}
g_form.setValue('u_weekdays', weekdaysCount);
}

 

The above script calculates the weekdays , excluding weekends.

 

SriharshaYe_2-1743684159189.png

 

  • Using Client Script. 
  • create a new onchange client script as below  
  • SriharshaYe_4-1743684331382.png
  •  The above Script calculates the weekdays and excludes the weekends