Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

sandhya morla
Giga Contributor

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);

}

find_real_file.png

I hope this will be helpful.

Kind Regards, 

Sandhya morla 

Comments
Dhanunjay2
Tera Guru

Hi,

I have tried above code, but Saturday and sundays are getting counted.

Version history
Last update:
‎03-05-2020 01:57 AM
Updated by: