- 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:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2024 09:20 AM
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!
- 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-04-2024 01:42 PM
Thanks so much! Works perfectly now!!!