GlideDateTime format issue

anirban300
Kilo Guru

Hello All,

 

I am facing an issue where we are receiving time through integration in the format (2023-09-07T17:37:56Z). So, what ever is the time sent , the date remains ok but the time is always set to 02:00:00.

Eg.

ReceivedSet by code
2023-09-07T17:37:56Z07/09/2023 02:00:00
2023-09-06T18:38:34Z06/09/2023 02:00:00

 

I am using the below code:

 

var gdt = new GlideDateTime(sourceValue); //sourceValue is the Received data from the above table
answer = gdt;
 
Is it possible to set the date in the system time zone or any specific time zone?
 
Regards
Anirban
3 REPLIES 3

Bert_c1
Kilo Patron

Hi Anirban,

 

Have a look here:

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideDateTimeAPI?navFil...

 

scroll down to see section "GlideDateTime(GlideDateTime gdt)" and transform the Received as necessary or get the sender to match a supported format.

Maik Skoddow
Tera Patron
Tera Patron

Hi @anirban300 

 

I know that issue and I have solved it by replacing the "T" with an empty char before passing it to the new GlideDateTime().

 

var gdt = new GlideDateTime(sourceValue.replaceAll('T', ' ')); 

 

Maik

DYCM
Mega Sage

Hi @anirban300 ,

1. GlideDateTime doesn't accept the format of the string "2023-09-07T17:37:56Z" as parameter for the constructor. Please check the following official article to get what are the formats it accepts

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideDateTimeAPI#r_GDT-...

1.png

 

2. You can set specific time zone for a GlideDateTime object.

Please play around with the following code snippet (My time zone is Hongkong, UTC+8)

 

gs.info("*** My Time Zone:"+gs.getUser().getTZ());
gs.info("*** My current time:"+gs.nowDateTime());


var sourceValue = "2023-09-07 17:37:56";
// When passing the date time string to GlideDateTime, it uses your local time zone
// My local time zone is Hongkong (+8)
var gdt = new GlideDateTime(sourceValue);
gs.info("*** Right after the object constructed:" + gdt.getDisplayValue());

gdt.setTimeZone("US/Eastern");
gs.info("*** After time zone switched to US/Eastern:" +gdt.getDisplayValue());


gdt.setTimeZone("Hongkong");
gs.info("*** After time zone switched to Hongkong:" + gdt.getDisplayValue());

 

2.png

FYI, you can quickly check what the available time zones you can use in the function by checking Time zone field in User3.png