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.

how to get the current date in client script ?

RudhraKAM
Tera Guru

we have a end date field , we need to validate the date and alert if the provided end date exceed 1 year from today 

I am using this client script and working fine in another catalog item , the only difference is in the working catalog item we are having a start date field and end date field ,, and we are calculating the difference and based on difference end date is greater that 1 year it will alert ,, 

 

How to set start date to current date  in on change client script 

function onChange(control, oldValue, newValue, isLoading) {
	
	var strtDate = g_form.getValue('csv_contractor_start_date');
	if (isLoading || newValue == '' || strtDate == '') {
		return;
	}
	
	
	var ga = new GlideAjax('getYearAgoDate');
	ga.addParam('sysparm_name','validateDateDurationOneYear');
	ga.addParam('sysparm_startdate', strtDate);
	ga.addParam('sysparm_enddate', newValue);
	ga.getXMLWait();
	
	var answer = ga.getAnswer();
	
	if (answer == true || answer == 'true') {
		g_form.hideFieldMsg('contractor_end_date',true);
	}
	else {
		g_form.clearValue('contractor_end_date');
		g_form.showFieldMsg('contractor_end_date','Maximum account duration is one year from the processing start date', 'error',true);
		
	}
}
1 ACCEPTED SOLUTION

Hi,

There is a mistake in yoru code. Try this and let me know.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '' || strtDate == '') {
		return;
	}
	
var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
	var strtDate = today_date_str;
	
	var ga = new GlideAjax('getYearAgoDate');
	ga.addParam('sysparm_name','validateDateDurationOneYear');
	ga.addParam('sysparm_startdate', strtDate);
	ga.addParam('sysparm_enddate', newValue);
	ga.getXMLWait();
	
	var answer = ga.getAnswer();
	g_form.addInfoMessage("Answer is "+answer);
	if (answer == true || answer == 'true') {
		g_form.hideFieldMsg('contractor_end_date',true);
	}
	else {
		g_form.clearValue('contractor_end_date');
		g_form.showFieldMsg('contractor_end_date','Maximum account duration is one year from the processing start date', 'error',true);
		
	}
}

 

Mark the comment as a correct answer and also helpful once worked.

View solution in original post

5 REPLIES 5

EvaH
Kilo Contributor

Very simple and working perfectly.  Thank you so much.