Catalog client script error on date check (date not in the past)

nmcl
Giga Expert

I have a catalog client script that is meant to be checking that the date selected is not in the past.

However, the message is displaying at all times - even if the date is in the future.

Script...

function onChange(control, oldValue, newValue, isLoading){

//Clear messages

g_form.hideFieldMsg('loan_required_from');

GlideUI.get().clearOutputMessages();

var msg;

//Get current date

var currentDateObj = new Date();

var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);

var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);

//Get start date

var startDateStr = g_form.getValue('loan_required_from');

var startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);

//Exits if date is empty

if (startDateStr == "") {

return;

}

if (startDateNum < currentDateNum) {

msg = 'Required from date/time cannot be in the past ';

alert(msg);

return false;

}

}

1 ACCEPTED SOLUTION

nmcl
Giga Expert

Hi all, this is now working using the below...



function onChange(control, oldValue, newValue, isLoading){



if (isLoading || newValue == '') {


return;


}



if(newValue != '')


{


var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));


var today = new Date();



if(reqDate.getTime() <= today.getTime()) {


alert('The loan required from date cannot be in the past');


g_form.setValue('loan_required_from','');


}


}


}


View solution in original post

4 REPLIES 4

Kalaiarasan Pus
Giga Sage

always do the date time comparison in the server side script is my suggestion .. Using date() tends to use your computer date time setting rather than your instance's date...



this could help ... Create a script include and add this to a functon



var futdate = new GlideDateTime();


var start   = new GlideDateTime(this.getParameter('sysparm_date'));


if(start < futdate)


{


return false;


}


else


{


return true;


}


RKumar3
Tera Guru

Hi Nat,


Please try below code.


var currentDateObj = new Date();


var currentDateStr = formatDate(currentDateObj, g_user_date_time_format);


var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_time_format);




//Get start date


var startDateStr = g_form.getValue('loan_required_from');


var startDateNum = getDateFromFormat(startDateStr, g_user_date_time_format);




//Exits if date is empty


if (startDateStr == "") {


  return;


}




var isEndBeforeStart = compareDates(currentDateStr, g_user_date_time_format, startDateStr, g_user_date_time_format);


if (isEndBeforeStart == 0) {


  msg = 'Required from date/time cannot be in the past ';


  alert(msg);


  return false;


} else {


  return true;


}




Below is the article you may want to refer to



Comparing Client Dates



Kalaiarasan P I think GlideDateTime is not available in client side scripting.



Regards,


Rajnish Kumar


nmcl
Giga Expert

Hi all, this is now working using the below...



function onChange(control, oldValue, newValue, isLoading){



if (isLoading || newValue == '') {


return;


}



if(newValue != '')


{


var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));


var today = new Date();



if(reqDate.getTime() <= today.getTime()) {


alert('The loan required from date cannot be in the past');


g_form.setValue('loan_required_from','');


}


}


}


davidshin
Kilo Explorer

I am trying to do something similar , but I want the date to be selected within one year or less. How would that script look like?