Date format issue string to Date/Time

Shekhar Deshin1
Mega Guru

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,

 

4 REPLIES 4

Astik Thombare
Tera Sage

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

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.

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

When i tested in PDI i can see same results

ShekharDeshin1_0-1702375947701.png

i need this date/time format should be - MM-DD-YYYY HH:MM:SS AM/PM