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 change month of date (numerical )to month name and set it in a field

Priya75
Tera Contributor

Hi All,

 
I have a date in format 22/12/2023 , I want to change the month in name, it should be 22/Dec/2023. 

 

 

Regards,

Priya

1 REPLY 1

Harish Bainsla
Kilo Patron
Kilo Patron

Hi try below code

function onChangeDate() {
var dateField = g_form.getValue('your_date_field'); // Replace 'your_date_field' with the actual field name

// Parse the date string to a JavaScript Date object
var dateObject = new Date(dateField);

// Get the month in three-letter abbreviation
var monthAbbreviation = getMonthAbbreviation(dateObject.getMonth());

// Format the date as DD/Mon/YYYY
var formattedDate = dateObject.getDate() + '/' + monthAbbreviation + '/' + dateObject.getFullYear();

// Set the formatted date back to the field
g_form.setValue('your_date_field', formattedDate);
}

function getMonthAbbreviation(monthIndex) {
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return monthNames[monthIndex];
}