GlideDateTime ignores AM/PM

qwertyflop
Giga Guru

I need to be able to convert a value from a catalog item field to a datetime to compare it to the current date. The problem is that GlideDateTime does not seem to recognize PM times, even though the API Docs say that GlideDateTime supports AM/PM timezones (e.g. dd.MM.yyyy hh:mm:ss a).

For instance:

gs.info(new GlideDateTime('2022-01-01 08:00:00 PM'));
gs.info(new GlideDateTime('01/01/2022 08:00:00 PM'));

// this code prints "2022-01-01 08:00:00", but it should print "2022-01-01 20:00:00"
1 ACCEPTED SOLUTION

qwertyflop
Giga Guru

I solved this by using getValue() in the client script, and GlideDateTime's .setDisplayValue method to create the GlideDateTime object with that display value.

View solution in original post

7 REPLIES 7

Hitoshi Ozawa
Giga Sage
Giga Sage

Anyways, ServiceNow will keep DateTime fields in yyyy-MM-dd HH:mm:ss format internally. So the DateTime from the catalog item should be in this format regardless of user's format.

Just be sure to use .getValue() to get the date/time field instead of .getDisplayValue().

 

qwertyflop
Giga Guru

I solved this by using getValue() in the client script, and GlideDateTime's .setDisplayValue method to create the GlideDateTime object with that display value.

GrantC
Tera Contributor

This doesn't necessarily pertain to your issue, but it is very similar. 

I couldn't get the "AM" (Meridiem) value to set correctly when attempting to set a field type of date/time on a form. 

I used the following code to format the GlideDateTime object correctly: 

var gdt = new GlideDateTime();//gdt is now a date object with the current date "yyyy-mm-dd"
gdt.addDaysLocalTime(1);//adds 1 day
gs.print(gdt.getLocalDate());
gdt.setDisplayValue(gdt.getLocalDate() + " 08:30:00 AM");//this part is what I was missing to set the AM/PM value.
gs.print(gdt.getDisplayValue());

I then used a glide record to get my form and update it using this line:

grUTMS.setValue('u_start_time', gdt);