Convert date variable to date in catalog client script

Tomi Corigliano
Kilo Sage

I need to compare a date variable with today's date but I can seem to convert the date variable to a date format;

var selectedDateStr = Date.parse(newValue);
	var selectedDateNum = getDateFromFormat(selectedDateStr,g_user_date_time_format); 
	
	var today_dateStr = formatDate(new Date(), g_user_date_time_format); 
	var todayDateNum = getDateFromFormat(today_dateStr, g_user_date_time_format);
	if(selectedDateNum < todayDateNum) {
		g_form.showErrorBox('pick_up_date','Expected date cannot be less than today date (selectedDateNum = ' + selectedDateNum + ' | todayDateNum = ' + todayDateNum +' | pick_up_date = ' + pick_up_date + ' | selectedDateStr = ' + selectedDateStr + ')',true);
	} else {
		g_form.hideFieldMsg('pick_up_date',true);
    }

 

ShowErrorBox:

Expected date cannot be less than today date (selectedDateNum = 0 | todayDateNum = 1715852782000 | pick_up_date = [object HTMLDivElement] | selectedDateStr = NaN)
1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage
2 REPLIES 2

Brian Lancaster
Tera Sage

You may want to take a look at this instead of using a script.

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

Or this one where you would use a script include called from a client script.

https://www.servicenow.com/community/in-other-news/auto-populating-and-validating-date-fields/ba-p/2...

Tomi Corigliano
Kilo Sage

Thanks, I needed to do it indeed in a script include instead of doing it in the client script.