GlideDateTime Conversation. From x timezone to UTC

erck
Tera Contributor

How do I set up a GlideDateTime in Eastern or Central and then return the UTC value? 

So 8:00 EST returns 13:00 UTC

5 REPLIES 5

TrevorK
Kilo Sage

GlideDateTime will always store the date as UTC. When you retrieve the GlideDateTime you are able to select what time zone you want it converted to. But you never changed how it's stored in UTC.

 

API: https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

Here is an example:

var gdt_utc = new GlideDateTime();
gs.info (gdt_utc); // UTC

gdt_utc.setTimeZone("Canada/Eastern"); // Change time zone
gs.info(gdt_utc.getDisplayValue()); // Show time in Canada/Eastern
gs.info (gdt_utc.getValue()); // Still UTC!!
 
Results:
*** Script: 2025-02-13 21:53:01
*** Script: 02-13-2025 16:53:01
*** Script: 2025-02-13 21:53:01
 Does this help with what you're looking to do?

erck
Tera Contributor

My requirement is to display business hours of each geo location. 

 

So how do I get 06am EST to return 11:00 UTC. 

5:00 CST to return 11 UTC

etc. 

 

If I try what you gave me, it translates from UTC to EST. 

 

var gdt_utc = new GlideDateTime('2025-02-13 06:00:00');
gs.info (gdt_utc); // UTC

gdt_utc.setTimeZone("Canada/Eastern"); // Change time zone
gs.info(gdt_utc.getDisplayValue()); // Show time in Canada/Eastern
gs.info (gdt_utc.getValue()); // Still UTC!!

OK, I think I understand. You have an EST and you want to be able to store it / translate it to UTC so you can display it in multiple time zones.

 

In the below you create a GlideDateTime object, then you set the time zone to EST, then you set the display value to the time you have (the known time). It has set the time zone object to EST but stores the time as UTC. You can then use the GlideDateTime API to retrieve it different ways, such as how I show you getting central time.

 

var gdt_utc = new GlideDateTime();
gdt_utc.setTimeZone("Canada/Eastern");
gdt_utc.setDisplayValue("02-13-2025 06:00:00");
gs.info (gdt_utc); // UTC
gs.info(gdt_utc.getDisplayValue()); // Show time in Canada/Eastern

gdt_utc.setTimeZone("Canada/Central");
gs.info(gdt_utc.getDisplayValue()); // Central
 
Result:
*** Script: 2025-02-13 11:00:00
*** Script: 02-13-2025 06:00:00
*** Script: 02-13-2025 05:00:00
 
The premise is that you are storing it as UTC time but you are setting the time zone to something for the "display value".
 
Am I understanding correctly and this is what you want?

Debasis Pati
Tera Guru

Hello,

You can try the below

var gdt_utc = new GlideDateTime(); // current time in UTC
gs.info("UTC: " + gdt_utc); // Output UTC time

// Set time to a specific time (e.g., 06:00 AM EST) in Canada/Eastern
gdt_utc.setValue("2025-02-14 06:00:00"); // Assuming the date is today
gdt_utc.setTimeZone("Canada/Eastern"); // Timezone set to EST

gs.info("EST Time: " + gdt_utc.getDisplayValue()); // Output time in EST

// Convert to UTC
var utc_time = gdt_utc.getValue(); // Get the equivalent UTC time
gs.info("Converted UTC: " + utc_time); // Display converted UTC