The CreatorCon Call for Content is officially open! Get started here.

Date function in client script is not working as expected

geogeorge
Tera Expert

Hi Team,

 

I have this code for validating Valid_to date in knowledge article and it is not entering into first if condition. Please suggest what went wrong.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var validToDate = g_form.getValue('valid_to');
var topic = g_form.getValue('topic');
var today = new Date();
alert(today);
var oneYearFromToday = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
var sixMonthsFromToday = new Date(today.getFullYear(), today.getMonth() + 6, today.getDate());
alert(topic);
alert(oneYearFromToday);
alert(sixMonthsFromToday);
if (validToDate > oneYearFromToday) {
alert("valid to is more that 1 year");
g_form.showErrorBox('Invalid date', 'Valid to date cannot be more than 1 year in the future');
g_form.setValue('valid_to', oldValue);
return;
}
if (validToDate < today) {
g_form.showErrorBox('Invalid date', 'Valid to date cannot be a past date');
g_form.setValue('valid_to', oldValue);
return;
}

if (topic === 'Known Error' && (validToDate < today || validToDate > sixMonthsFromToday)) {
g_form.showErrorBox('Invalid date', 'Valid to date cannot be a past date or more than 6 months in the future for known errors');
g_form.setValue('valid_to', oldValue);
return;
}
}

 

 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @geogeorge ,

It is because of the type difference between the validToDate which you retrieved from the form and the oneYearFromToday is JavaScript date type.

Try converting the validToDate to JS date object type. Like below,

 

var validToDate = g_form.getValue('valid_to');

validToDate = new Date(validToDate + '');

 

Then try comparing them. And also check all the variables validToDate and oneYearFromNow are having the intended values by using an alert, before the if condition.

 

Thanks,

Anvesh

Thanks,
Anvesh

View solution in original post

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @geogeorge ,

It is because of the type difference between the validToDate which you retrieved from the form and the oneYearFromToday is JavaScript date type.

Try converting the validToDate to JS date object type. Like below,

 

var validToDate = g_form.getValue('valid_to');

validToDate = new Date(validToDate + '');

 

Then try comparing them. And also check all the variables validToDate and oneYearFromNow are having the intended values by using an alert, before the if condition.

 

Thanks,

Anvesh

Thanks,
Anvesh

HI @AnveshKumar M ,

Can we pass current date into a label field of UI builder