"gr.getValue()" and "gr.getByFormat('hh:mm:ss')" return different times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 01:56 PM
gs.info(" time is " + gt.getByFormat('hh:mm:ss')); // *** Script: time is 05:00:00
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 02:11 PM
The discrepancy you're observing between the outputs of getValue and getByFormat methods in your script appears to be related to time zone handling.
getValue Method: This method returns the date and time in UTC (Coordinated Universal Time). Your output of 1970-01-01 17:00:00 is in UTC.
getByFormat Method: This method returns the time formatted according to the specified format, in this case, 'hh:mm:ss'. However, it seems to be returning the time in your local time zone or the time zone configured in your ServiceNow instance, which appears to be 12 hours behind UTC. Hence, the output is 05:00:00.
GlideDateTime and GlideTime classes handle time zones, and it's essential to be mindful of this when working with date and time in ServiceNow. The GlideDateTime object (gdt in your script) represents a specific moment in time, and the time zone can impact how that moment is represented.
To resolve this discrepancy, you have a couple of options:
Standardize Time Zones: Ensure that all your date-time manipulations and representations are in a single time zone (either UTC or your local time zone). You can use methods like setTZ to explicitly set the time zone.
Adjust the Format Method: If you want to keep using local time, adjust the getByFormat method to ensure it's clear which time zone you are displaying.
If you need the time in a specific time zone, you should explicitly set or convert the time zone before retrieving the time. ServiceNow offers methods to handle time zone conversions which can be used to ensure consistency in your script's output.
MVP 2025 ✨
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2023 02:16 PM - edited 11-16-2023 02:17 PM
Hi @PB7
Your field is not returning value in string so it is taking old 1970s date randomly in GlideRecord.
As your gdt variable stores the value 1970-01-01 17:00:00.
- gdt.getValue() returns the same time 17:00:00.
- gdt.grtByFormat(‘hh:mm:ss’) works with 12 hrs am pm cycle. So it is returning 05:00:00.
- gdt.getByFormat(‘HH:MM:SS’) works 24 hrs cycle. It will return 17:00:00 only.
Now to get the proper date always use below format:
var gdt = new GlideDateTime(); //if we pass date here, it will convert automatically to system timezone
gdt.setDisplayValue(ct.u_previous_task_date.toString());
gs.addInfoMessage(gdt.getTime());
Thanks!
Please mark this response as correct and helpful ✔️👍
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023 09:35 AM
I will do so ; still trying to get it to work. I'm not seeing any output when I run the bottom script.
I get this to the console:
Run Fix Script
*** Script: CT-test 1
*** Script: Coverage Ticket template number is COT0001758
[0:00:00.017] Total Time
-- this is the FixScript I'm running:
gdt.setDisplayValue(ct.u_previous_task_date.toString());
gs.addInfoMessage(gdt.getTime());
}
+++
Any thoughts?