setDisplayValue is not converting this date format properly 'MM/dd/yy HH:mm:ss'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 05:44 AM
When attempting to convert a string in this format 'MM/dd/yy HH:mm:ss' to 'yyyy-MM-dd HH:mm:ss' setDisplayValue does not return the proper year.
Example:
Original: 01/02/25 10:50:40
Returns: 0025-01-02 15:50:40
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:15 AM - edited 01-03-2025 06:17 AM
Hello @Brian Terrick ,
You can try as below code.
// Function to convert 'MM/dd/yy HH:mm:ss' to 'yyyy-MM-dd HH:mm:ss'
function convertDateString(inputDate) {
// Split the input date into components (MM/dd/yy HH:mm:ss)
var parts = inputDate.split(/[/ :]/);
var month = parts[0].padStart(2, '0'); // Ensure month is 2 digits
var day = parts[1].padStart(2, '0'); // Ensure day is 2 digits
var year = parts[2]; // Extract the year (2 digits)
var hour = parts[3].padStart(2, '0'); // Ensure hour is 2 digits
var minute = parts[4].padStart(2, '0'); // Ensure minute is 2 digits
var second = parts[5].padStart(2, '0'); // Ensure second is 2 digits
// Convert 2-digit year to 4-digit year
if (year < 50) {
year = '20' + year; // Assuming years 00-49 are in the 21st century (2000-2049)
} else {
year = '19' + year; // Years 50-99 will be in the 20th century (1900-1999)
}
// Construct the new date string in 'yyyy-MM-dd HH:mm:ss' format
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
// Example input date in 'MM/dd/yy HH:mm:ss' format
var inputDate = '01/02/25 10:50:40';
// Call the function and store the result
var convertedDate = convertDateString(inputDate);
// Log the converted date
gs.info('Original Date: ' + inputDate);
gs.info('Converted Date: ' + convertedDate);
I have tried this in the background script, and it gives an output as below:
*** Script: Original Date: 01/02/25 10:50:40
*** Script: Converted Date: 2025-01-02 10:50:40
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:16 AM - edited 01-03-2025 06:20 AM
It's simple to set the value with the right format when you use the setDisplayValue function. Please refer to the screenshot below
adding code snippet for reference,
var dateTime = GlideDateTime();
// Your original time will go as the first arguement and second arguement represent the format
dateTime.setDisplayValue("01/02/25 10:50:40","MM/dd/yy HH:mm:ss");
gs.info(dateTime.getDisplayValue()); // This line prints the Date time display value
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 07:51 AM
Hello @Brian Terrick
- I believe you were trying the following:
- In this case, the system is unable to recognize the year 25 as being in the current century (2025) and instead interprets it as the year 0025.
- However, by modifying the script as suggested by @Vasantharajan N , you can resolve this issue. Here’s the corrected script:
var dateTime = GlideDateTime();
dateTime.setDisplayValue("01/02/25 10:50:40","MM/dd/yy HH:mm:ss");
gs.info(dateTime.getDisplayValue());
- This will ensure the system correctly recognizes and handles the year as 2025.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2025 05:40 PM
Hello @Brian Terrick
Thank you for marking my response as helpful.
You can also mark it as an accepted solution, it helps future readers to locate the solution easily in community.
Thank You
Juhi Poddar