Date format issue string to Date/Time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:32 AM
String Field - Exp Date : - 2023-03-22T17:49:57:0000000Z
Date/Time Field - Expiry Date : 12-21-2023 11:15:55 AM
We have requirement about we need to convert above Exp date string format to Date/Time please see the above example.in the 24Hrs Format.
Please suggest,
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:51 AM
Hi @Shekhar Deshin1 ,
In ServiceNow, you can use the GlideDateTime API to convert a date string to a formatted date and time. Here's an example script that you can use to achieve this:
// Get the xString Field value
var xStringFieldValue = '2023-03-22T17:49:57:0000000Z'; // Replace this with your actual field value
// Create a new GlideDateTime object from the xString Field value
var glideDateTime = new GlideDateTime(xStringFieldValue);
// Format the date and time in 24-hour format (HH:mm:ss)
var formattedDateTime = glideDateTime.getDisplayValueInternal(); // This method returns the formatted date and time
// Log the result
gs.info('Formatted Date/Time: ' + formattedDateTime);
output-
2023-03-21 17:00:00
Please mark ✅ Correct if this resolves your issue, and also mark 👍 Helpful if you find my response valuable based on its impact.
Regards,
Astik Thombare
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:33 AM
Thanks for Response i need this date/time format - MM-DD-YYYY HH:MM:SS AM/PM
Expiry Date : 12-21-2023 11:15:55 AM
I need output in above format i mentioned.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:50 AM
Hello @Shekhar Deshin1
In the above code which is provided by @Astik Thombare4 you can keep the same code just but replace this line "ar formattedDateTime = glideDateTime.getDisplayValueInternal();" with this "var formattedDateTime = glideDateTime.getDisplayValue();"
// Get the xString Field value
var xStringFieldValue = '2023-03-22T17:49:57:0000000Z'; // Replace this with your actual field value
// Create a new GlideDateTime object from the xString Field value
var glideDateTime = new GlideDateTime(xStringFieldValue);
// Format the date and time in MM-DD-YYYY HH:mm:ss AM/PM format
var formattedDateTime = glideDateTime.getDisplayValue(); // This method returns the formatted date and time
// Log the result
gs.info('Formatted Date/Time: ' + formattedDateTime);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Regards,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 02:13 AM
When i tested in PDI i can see same results
i need this date/time format should be - MM-DD-YYYY HH:MM:SS AM/PM