- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 06:33 PM
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"
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 01:23 PM
I solved this by using getValue() in the client script, and GlideDateTime's .setDisplayValue method to create the GlideDateTime object with that display value.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 08:03 PM
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().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 01:23 PM
I solved this by using getValue() in the client script, and GlideDateTime's .setDisplayValue method to create the GlideDateTime object with that display value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2022 09:23 AM
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);