GlideDateTime format issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 07:47 AM
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.
Received | Set by code |
2023-09-07T17:37:56Z | 07/09/2023 02:00:00 |
2023-09-06T18:38:34Z | 06/09/2023 02:00:00 |
I am using the below code:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 01:15 PM
Hi Anirban,
Have a look here:
scroll down to see section "GlideDateTime(GlideDateTime gdt)" and transform the Received as necessary or get the sender to match a supported format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 06:38 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 11:05 PM - edited 09-15-2023 11:06 PM
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
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());
FYI, you can quickly check what the available time zones you can use in the function by checking Time zone field in User