Convert date/time in String field to Date/time field data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:21 AM
I have data in expiration date field in string format, i have to convert it to date and time to update it to Expiry date field.
same goes with below screenshot also
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 12:31 AM - edited 12-12-2023 12:57 AM
Hi @SATHISH KUMAR 2 ,
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:10 AM
The output should be in format of MM-DD-YYYY HH:mm:ss AM?PM format not in YYYY-MM-DD format. Please suggest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 01:35 AM
Hello @SATHISH KUMAR 2
I the above code which is provided by @Astik Thombare 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 01:50 AM
Please modify the script as below
// 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);
var gt = new GlideDateTime(formattedDateTime);
var day =gt.getDayOfMonthUTC();
var month =gt.getMonthLocalTime();
var year =gt.getYearLocalTime();
var time =gt.getTime();
var timeCreated = time.getByFormat('HH:mm:ss');
// Set the date format
var finalDateTime = day + '-' + month + '-' + year + " " + timeCreated;
gs.info('finalDateTime' +' '+ finalDateTime);
output -
21-3-2023 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