- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:03 AM
Please could someone help as to how to convert date "August 20th, 2024 16:58:34" into YYYY-MM-DD HH:MM:SS.
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:34 AM
Hi @bpolo ,
Updated the Regex. Below are the updated code:
// Assuming 'dateString' is the input date in the format "August 20th, 2024 16:58:34"
var dateString = "August 20th, 2024 16:58:34";
// Remove the 'th', 'st', 'nd', 'rd' from the date string to avoid parsing issues
dateString = dateString.replace(/(\d+)(st|nd|rd|th),/, "$1");
// Manually parse the date string
var months = {
January: '01',
February: '02',
March: '03',
April: '04',
May: '05',
June: '06',
July: '07',
August: '08',
September: '09',
October: '10',
November: '11',
December: '12'
};
var dateParts = dateString.split(' ');
var year = dateParts[2];
var month = months[dateParts[0]];
var day = dateParts[1].length === 1 ? '0' + dateParts[1] : dateParts[1]; // Ensure two digits
var time = dateParts[3];
// Combine into a YYYY-MM-DD format
var formattedDate = year + '-' + month + '-' + day + ' ' + time;
// Create a GlideDateTime object
var gdt = new GlideDateTime(formattedDate);
// Output the result
gs.info(gdt.getValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 01:29 PM
Hello @bpolo
I suggest avoiding the use of split for date conversion, as it could lead to issues if the date format changes on the platform further. Instead, I suggest using the script provided in the following link :
Best regards,
Hajar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:22 AM
Thanks for sending this info. So unfortunately, the article did not help with the date format that I am needing to convert.