date format getting changed in onchange in client script

Simona6
Tera Contributor

Two fields on form start date and end date.Expecting as we fill start date(date), end date(date) will be automatically populate date of next two days. 

Executing Onchange Client script calling script include but the date format for end date is not same as start date.

e.g if start date is 11/10/2023 then end date is 11-12-2023

 

Any solution.

1 ACCEPTED SOLUTION

@Simona6 

use this onchange client script on start date

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		return;
	}
	var dateMS = getDateFromFormat(newValue, g_user_date_format);
	dateMS += 2 * 24 * 60 * 60 * 1000; // 2 here indicates days count
	var newDT = new Date();
	newDT.setTime(dateMS);
	g_form.setValue('u_review_end_date',formatDate(newDT, g_user_date_format));
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Lithesh
Kilo Sage

Hi @Simona6 ,

We should use the GLIDEDATETIME API for this,

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

var start = g_form.getValue('start_date');
var end = new GlideDateTime();
end.setDisplayValue(start);
end.addDaysLocalTime(2);
g_form.setValue('end_date', end);
}

If this helps you kindly mark it as correct.

Simona6
Tera Contributor

Glidedatetime API can't be used in client script. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Simona6 

are both the fields of type date?

can you share screenshot?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Simona6_0-1683876714497.png

@Ankur Bawiskar these are two field with type-date