GlideDateTime().getDisplayValue() returns inconsistent date format

Dejan
Tera Expert

developer.servicenow.com says that the getDisplayValue() returns: 'The date and time in the user's format and time zone. Keep in mind when designing business rules or script includes that this method may return values in different formats for different users.'

We have this code in a script include which gets invoked by a business rule. We are setting the time zone based on the asset location and retrieving the date with getDisplayValue() to get the date/time in the desired time zone. For a couple of dates we received a different date format (the time zone is fine).

Example:

var gdt = new GlideDateTime(current.sys_created_on);
var tz = Packages.java.util.TimeZone.getTimeZone('America/New_York');
gdt.setTZ(tz);
gdt.getDisplayValue();

getDisplayValue() returns most of the time the date in the format '2021-04-22 11:12:13' ((yyyy-MM-dd HH:mm:ss) and sometimes '04-22-2021 11:12:13' (MM-dd-yyyy HH:mm:ss).

Could someone please explain why is this happening and what can we do to enforce a specific date format?

1 ACCEPTED SOLUTION

Aman Singh_
Kilo Sage

Hi,

 

To get the specific format can use,

for example

var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();

gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));

 

Please mark Correct/helpful???? if applicable, thanks!!

Aman

View solution in original post

2 REPLIES 2

Aman Singh_
Kilo Sage

Hi,

 

To get the specific format can use,

for example

var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();

gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));

 

Please mark Correct/helpful???? if applicable, thanks!!

Aman

Thank you Aman!