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

parvinder2
Tera Expert

Hi Jim,



How is the date being set in the field you're referring to and what method(s) are you using to display it in both the catalog item and portal?


Typically UTC is the default format (yyyy-MM-dd HH:mm:ss).



I would suggest looking at the following links with regards to format and setting/getting values.



http://wiki.servicenow.com/index.php?title=Using_Date_and_Time_Fields#Date_Format


GlideDateTime - ServiceNow Wiki



Please mark Correct or helpful.



Regards,


Parvinder


We are calling the fields on the server-side script using gr.resolved_at and gr.closed_at. Can you provide a sample code that can convert the date/time values of those field to something like "July 11, 2017 12:35PM"


Please use below sample code to convert date format



  1. var temp = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  2. var cdt = gr.closed_at;  
  3. cdt = temp.parse(cdt);  
  4. var output = new Packages.java.text.SimpleDateFormat("MM/dd/yyyy k:mm:ss z");  
  5. output = output.format(cdt);  



Regards,


Sachin


Hello, we tried this and it works but it shows time in GMT regardless of user timezone preference - do you have an idea of how we can take into account the user's timezone (or system timezone)