Compare two dates in Catalog Client Script

JJG
Kilo Guru

Hello,

I have 3 date fields on my record producer.

1) Start date - 'pay_period_start'

2) End date - 'pay_period_end'

2) Date - 'date'

I need to compare the 'date' with the Start Date & End date. This will decide what happens on the form. Here is my on change client script so far. it is not working:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var dateValue = g_form.getValue('date');
var start = g_form.getValue('pay_period_start');
var end = g_form.getValue('pay_period_end');

if (dateValue < start || dateValue > end) {
g_form.showFieldMsg('date', ':Please provide an explanation.', 'error');
g_form.setVisible('past_date_hours_explanation', true);
g_form.setMandatory('past_date_hours_explanation', true);
}

else {
g_form.hideFieldMsg('date');
g_form.setMandatory('past_date_hours_explanation', false);
g_form.setVisible('past_date_hours_explanation', false);
}
}

1 ACCEPTED SOLUTION

use glide ajax here, and in client callable script include compare it, based on that return true or false , you will get more flexibility in date comparison  if you will use script include + glideajax.

 

sample code mentioned in below thread. 

https://community.servicenow.com/community?id=community_question&sys_id=56c70fe9db1cdbc01dcaf3231f96...

View solution in original post

7 REPLIES 7

all 3 variables are date/time type variable ? 

They are not date/time variables, they are only date variables

use glide ajax here, and in client callable script include compare it, based on that return true or false , you will get more flexibility in date comparison  if you will use script include + glideajax.

 

sample code mentioned in below thread. 

https://community.servicenow.com/community?id=community_question&sys_id=56c70fe9db1cdbc01dcaf3231f96...