Sometimes databases don’t store their dates in UTC. I recently ran across a system that stored all their dates in Eastern time, or at least, that’s what time zone they were in when they came out of the API (because the world revolves around Eastern!).
In ServiceNow, dates are stored in UTC, so when writing dates from this third party system, I had to specify that the dates being written were Eastern. GlideDateTime does have setDisplayValue, however this uses the time zone of the logged in user. My function would be used by a job or by users in different time zones, so I couldn’t rely on the user’s time zone to always be Eastern.
After a lot of fumbling around through articles on GlideDateTime and time zones, I found this [recent] article which explains things simply https://www.servicenow.com/community/in-other-news/demystifying-glidedatetime-and-timezones-also-convert-dates-to/ba-p/2472586#:~:text=the%20following%20code%20will%20return,%3B%20gs.info(gdt. The article tipped me off to the method setTimeZone. NOTE THAT THIS METHOD DOES NOT WORK IN SCOPED APPS. You have to create a script include in Global and call it from the scoped app.
Below is a sample script which sets at GlideDateTime to midnight on 2024-07-01 in both New York and Los Angeles. It then displays the corresponding UTC time.
var newyork = new GlideDateTime();
newyork.setTimeZone('America/New_York');
newyork.setDisplayValue('2024-07-01', 'yyyy-MM-dd');
gs.print('2024-07-01 00:00:00 in New York is ' + newyork.getValue() + ' UTC');
var la = new GlideDateTime();
la.setTimeZone('America/Los_Angeles');
la.setDisplayValue('2024-07-01','yyyy-MM-dd');
gs.print('2024-07-01 00:00:00 in Los Angeles is ' + la.getValue() + ' UTC');
If your date is coming from ServiceNow, you’ll want to use the getValue method and specify the format:
newyork.setDisplayValue(current.getValue(‘u_date_field_no_time’, ‘yyyy-MM-dd’);
A list of Time Zones can be found here https://docs.oracle.com/middleware/1221/wcs/tag-ref/MISC/TimeZones.html