We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

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];
}