How to convert the date/time format to MM/DD/YYYY HH:MM:SS?

Bird1
Mega Sage

Hello,

 

I need help. I tried to get the date/time data from a field in Change Request. I tried to use getDisplayValue before but the result is the date/time format is not stable. Sometimes got

1. MM/DD/YYYY HH:MM:SS or 

2. MM.DD.YYYY HH:MM:SS or 

3. MM-DD-YYYY HH:MM:SS

 

I assumed the unstable of DisplayValue because we received some record from POST API, they may use another format, so that cause of problem.

 

So, I switch using getValue instead but the format is not satisfied for user YYYY-MM-DD HH:MM:SS, so I tried to find out the solution to convert the date/time format from getValue (YYYY-MM-DD HH:MM:SS) to MM/DD/YYYY HH:MM:SS.

 

Sample Script:

var gr = new GlideRecord('change_request');
gr.addQuery('number', 'CHG0058582');
gr.query();

if (gr.next())

{

gs.print(gr.start_date.getValue());

}

 

 

Result: 

 2023-05-04 23:00:00

 

What I need result is 05/04/2023 23:00:00 (MM/DD/YYYY HH:MM:SS format).

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hello@Bird1,

 

Try this updated scripts. It will gives you date time in required format.

 

var chgRecord = new GlideRecord('change_request');
chgRecord.addQuery('number', 'CHG0058582');
chgRecord.query();
if (chgRecord.next()) {

	var start_date = chgRecord.start_date.getValue();
	gs.info("start_date: " + start_date);

	var today = new GlideDateTime(start_date);
	var day = today.getDayOfMonthUTC();
	var month = today.getMonthLocalTime();
	var year = today.getYearLocalTime();
	var time = today.getTime();
	var timeCreated = time.getByFormat('HH:mm:ss');

	// Set the date format
	var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;
	gs.info('requiredDate: ' + requiredDate);
}

 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

 

The world works with ServiceNow

View solution in original post

5 REPLIES 5

Samaksh Wani
Giga Sage
Giga Sage

Hello @Bird1 

 

 

 

 

 

 

 

var gr = new GlideRecord('change_request');
gr.addQuery('number', 'CHG0058582');
gr.query();
var date;
if (gr.next())
{

date=gr.start_date.getValue();
date=date.getByFormat("MM/dd/yyyy HH:mm:ss");
gs.print(date);

}

 

 

 

 

Screenshot 2023-07-22 at 11.18.53 AM.png

 

 

Plz use this Script for your expected output.

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Regards,

Samaksh

 

Sagar Pagar
Tera Patron

Hello@Bird1,

 

Try this updated scripts. It will gives you date time in required format.

 

var chgRecord = new GlideRecord('change_request');
chgRecord.addQuery('number', 'CHG0058582');
chgRecord.query();
if (chgRecord.next()) {

	var start_date = chgRecord.start_date.getValue();
	gs.info("start_date: " + start_date);

	var today = new GlideDateTime(start_date);
	var day = today.getDayOfMonthUTC();
	var month = today.getMonthLocalTime();
	var year = today.getYearLocalTime();
	var time = today.getTime();
	var timeCreated = time.getByFormat('HH:mm:ss');

	// Set the date format
	var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;
	gs.info('requiredDate: ' + requiredDate);
}

 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

 

The world works with ServiceNow

Hi @Bird1,

 

No need to split the time and concatenate again.

This will also work.

var requiredDate = day + '/' + month + '/' + year + " " + timeCreated;

 

removed the unwanted lines and updated with correct.

var Timesplit = timeCreated.split(":");
var strTime = Timesplit[0] + ":" + Timesplit[1] + ":" + Timesplit[2];

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

 

The world works with ServiceNow

Thank you very much!