Simplest way to convert Date and Time formats on server-side script on Service Portal?

JC S_
Mega Guru

We are creating a custom widget that contains the incident resolution time (gr.resolved_at) and incident close time (gr.closed_at) but want to display it in different date time format (like July 11, 2017 12:35PM) instead of the system default 2017-07-11 12:35:00 (note that we only want to do this on this widget so changing the system default date/time format is not an option.

We need to adjust the time shown depending on user time zone as well.

1 ACCEPTED SOLUTION

JC S_
Mega Guru

Hello everyone! Thanks for all the help, we are now able to implement this properly using solution provided by sachin.namjoshi and we added a little adjustment to consider user's time zone.



This code will get the display value instead, which is already on the user's timezone. Although this will not show the correct timezone text, the values show for date and time is properly adjusted to user's timezone:



// Convert Date-Time Format


var temp = new Packages.java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); //This defines the format of input values, check date-time format of field first


var output = new Packages.java.text.SimpleDateFormat("MMMM dd, yyyy h:mm a"); //Change the date-time format here to what you want


var cdt, rdt, closed, resolved;



cdt = gr.closed_at.getDisplayValue();


cdt = temp.parse(cdt);


closed = output.format(cdt);



rdt = gr.resolved_at.getDisplayValue();


rdt = temp.parse(rdt);


resolved = output.format(rdt);



Now you can just use the variables resolved and closed anywhere you want to display the date/time formatted to the format you setup.



Sample input: 07/12/2017 21:30:00


Sample output: July 12, 2017 9:30 PM


View solution in original post

6 REPLIES 6

lSurya 24
Giga Guru

Hello Jimboy,



Below link is my thread which might be helpful,



how to customize Date format in HR Service Portal?


JC S_
Mega Guru

Hello everyone! Thanks for all the help, we are now able to implement this properly using solution provided by sachin.namjoshi and we added a little adjustment to consider user's time zone.



This code will get the display value instead, which is already on the user's timezone. Although this will not show the correct timezone text, the values show for date and time is properly adjusted to user's timezone:



// Convert Date-Time Format


var temp = new Packages.java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); //This defines the format of input values, check date-time format of field first


var output = new Packages.java.text.SimpleDateFormat("MMMM dd, yyyy h:mm a"); //Change the date-time format here to what you want


var cdt, rdt, closed, resolved;



cdt = gr.closed_at.getDisplayValue();


cdt = temp.parse(cdt);


closed = output.format(cdt);



rdt = gr.resolved_at.getDisplayValue();


rdt = temp.parse(rdt);


resolved = output.format(rdt);



Now you can just use the variables resolved and closed anywhere you want to display the date/time formatted to the format you setup.



Sample input: 07/12/2017 21:30:00


Sample output: July 12, 2017 9:30 PM