- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 01:25 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 01:35 AM
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);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 04:05 AM
Hello @Abhijit Das7
Just run below script -
var dateString = "Fri Feb 27 2025";
// Split the string to extract the required parts
var parts = dateString.split(" ");
var monthStr = parts[1]; // "Feb"
var day = parts[2]; // "27"
var year = parts[3]; // "2025"
// Create a month mapping for conversion
var monthMap = {
"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
"May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
"Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"
};
// Convert to proper format "yyyy-MM-dd"
var formattedDate = year + "-" + monthMap[monthStr] + "-" + day;
// Convert to GlideDateTime
var gdt = new GlideDateTime(formattedDate);
gs.info(gdt.getDate().getByFormat('yyyy-MM-dd'));
Kindly mark my answer as helpful and accept solution if it helped you in anyway,
,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwN
eEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 07:30 PM
Hello @Abhijit Das7
Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway, so that it can move from unsolved bucket to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2025 09:54 PM
@Abhijit Das7 I tried in background script below script:
function convertToYYYYMMDD(dateString) {
// Parse the input date string
var dateObj = new Date(dateString); // JavaScript Date object
// Create a GlideDateTime object and set the parsed date
var gdt = new GlideDateTime();
gdt.setValue(dateObj.getFullYear() + '-' +
(dateObj.getMonth() + 1).toString().padStart(2, '0') + '-' +
dateObj.getDate().toString().padStart(2, '0'));
gs.info(gdt.getValue());
}
convertToYYYYMMDD("Fri Feb 27 2025");
Please mark correct/helpful if this helps you!