How to change date format.

Abhijit Das7
Tera Expert

Hi Everyone,

 

I am getting date in this format: Fri Feb 27 2025, as a string and want to convert it into 2025-02-27 in script include. How can I change format of the date?

 

Thanks in advance 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

something like this in server side

function convertDateFormat(dateString) {
        // Parse the date string
        var dateParts = dateString.split(' ');
        var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var month = monthNames.indexOf(dateParts[1]) + 1;
        var day = dateParts[2];
        var year = dateParts[3];

        // Format date as YYYY-MM-DD
        var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
        
        return formattedDate;
    }


var inputDate = "Fri Feb 27 2025";
var outputDate = convertDateFormat(inputDate);
gs.info(outputDate);

AnkurBawiskar_0-1742286918815.png

 

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

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

something like this in server side

function convertDateFormat(dateString) {
        // Parse the date string
        var dateParts = dateString.split(' ');
        var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var month = monthNames.indexOf(dateParts[1]) + 1;
        var day = dateParts[2];
        var year = dateParts[3];

        // Format date as YYYY-MM-DD
        var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
        
        return formattedDate;
    }


var inputDate = "Fri Feb 27 2025";
var outputDate = convertDateFormat(inputDate);
gs.info(outputDate);

AnkurBawiskar_0-1742286918815.png

 

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

@Abhijit Das7 

Hope you are doing good.

Did my reply answer your question?

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

@Abhijit Das7 

Hope you are doing good.

Did my reply answer your question?

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

@Abhijit Das7 

Hope you are doing good.

Did my reply answer your question?

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