Convert date/time in String field to Date/time field data

SATHISH KUMAR 2
Tera Contributor

SATHISHKUMAR2_0-1702369138555.png

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

SATHISHKUMAR2_1-1702369251598.png

 

4 REPLIES 4

Astik Thombare
Tera Sage
Tera Sage

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

The output should be in format of MM-DD-YYYY  HH:mm:ss AM?PM format not in YYYY-MM-DD format. Please suggest

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

Astik Thombare
Tera Sage
Tera Sage

Hi @SATHISH KUMAR 2 

 

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