Changing date format in Service Portal Catalog Item Variable from "dd/MM/yyyy" to "MM/dd/yyyy"

Von Naval
Tera Contributor

Hello. I have a catalog item that has a variable date field called Expected Date (expected_date) where it inputs a date with the default format of "DD/MM/YYYY".

VonAubreyNava_0-1676941791938.png

I want to make it so that the default value and any other value inputted in this field is always in a "MM/DD/YYYY" format without the need to change the System Property of the date format in the instance.

 

Is there any way to achieve this?

10 REPLIES 10

Hammi
Tera Contributor

You can achieve this by configuring the catalogue item Client Script:

Type: onLoad or onChange (depending on when you want the script to run).

 

Script:

function onLoad() {
formatDateToDDMMYYYY();
}

function formatDateToDDMMYYYY() {
var Expected Date = g_form.getValue('expected_date');
if (Expected Date) {
var dateObj = new Date(startDate);
var day = String(dateObj.getDate()).padStart(2, '0');
var month = String(dateObj.getMonth() + 1).padStart(2, '0'); // Months are zero-indexed
var year = dateObj.getFullYear();
var formattedDate = day + '-' + month + '-' + year;
g_form.setValue('expected_date', formattedDate);
}
}