Convert date format "August 20th, 2024 16:58:34" into YYYY-MM-DD HH:MM:SS

bpolo
Tera Guru

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! 

1 ACCEPTED SOLUTION

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()); 

View solution in original post

6 REPLIES 6

debendudas
Mega Sage

Hi @bpolo , you can use the below 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()); // This will return the date in "YYYY-MM-DD HH:MM:SS" format

Thanks so much for this solution!!! So the date piece is working well, but the time reverts to zeroes. The answer I get is 2024-08-20 00:00:00.

Any thoughts what could be causing the time to be set to zeroes? Thanks again!

 

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()); 

Thanks so much! Works perfectly now!!!