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

Maik Skoddow
Tera Patron
Tera Patron

Hi @qwertyflop 

internally GlideDateTime is already in 24-hours format.

Try

gs.info(new GlideDateTime('01/01/2022 20:00:00'));

 

Kind regards
Maik

Hitoshi Ozawa
Giga Sage
Giga Sage

>though the API Docs say that GlideDateTime supports AM/PM timezones (e.g. dd.MM.yyyy hh:mm:ss a).

Where is in the docs?

I can only find the following in both global and scoped.

GlideDateTime(String value)

Instantiates a new GlideDateTime object from a date and time value in the UTC time zone specified with the format yyyy-MM-dd HH:mm:ss.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_APIRef#r_ScopedGl...

Hitoshi Ozawa
Giga Sage
Giga Sage

If the question is referring to the below, the argument is in GlideDateTime format and not String.

GlideDateTime(GlideDateTime gdt)

Instantiates a new GlideDateTime object set to the time of a specified GlideDateTime object in GMT format.

The system attempts to match the argument passed to the gdt parameter with the specified internal system format. If the argument does not match the system format, the system attempts to match it to one of the following formats in this order:

  • yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • MM/dd/yyyy HH:mm:ss
  • MM-dd-yyyy HH:mm:ss
  • MM-dd-yyyy HH:mm
  • MM-dd-yyyy
  • MM/dd/yy HH:mm:ss
  • MM/dd/yyyy
  • dd-MM-yyyy HH:mm:ss
  • dd-MM-yyyy HH.mm.ss
  • dd-MM-yyyy HH.mm
  • dd-MM-yy HH:mm:ss
  • dd-MM-yy HH.mm.ss
  • dd/MM/yyyy
  • dd-MM-yyyy
  • yyyy-MM-dd HH:mm
  • yyyy-MM-dd
  • dd.MM.yyyy HH:mm:ss
  • dd.MM.yyyy HH.mm.ss
  • dd.MM.yyyy hh:mm:ss a
  • dd.MM.yyyy hh.mm.ss a
  • dd.MM.yyyy

 

Hitoshi Ozawa
Giga Sage
Giga Sage

JavaScript's Date does support AM/PM so the following will create a GlideDateTime 

var d = new Date('01/01/2022 08:00:00 PM');
gs.info(d);
var gdt = new GlideDateTime(d);
gs.info(gdt);

Result. Note that Date() is returning date in PST timezone and GlideDateTime() expects date/time in UTC.

*** Script: Sat Jan 01 2022 20:00:00 GMT-0800 (PST)
*** Script: 2022-01-02 04:00:00