Time conversion between different timezones in scoped application

Anjali Priya
Tera Contributor

I have a requirement where I need to write a script to convert CET timezone to a dynamic (user input) timezone value, in a scoped application. I have tried using setTimeZone() but it doesn't seem to be working for scoped applications. 

 

Any response or help on this would be greatly appreciated.

Thanks!

 

3 REPLIES 3

Harmeet2Singh
Tera Expert

var usertimezone = "Europe/London";

var ndt = new GlideDateTime();
gs.info("System Time is " + ndt);
var newtimezone = new GlideScheduleDateTime(ndt);
newtimezone.setTimeZone(usertimezone);
gs.info("User set time is " + newtimezone.getGlideDateTime().getDisplayValue());

Ankur Bawiskar
Tera Patron
Tera Patron

@Anjali Priya 

setTimeZone() does work in scoped app

var now = new GlideDateTime();
gs.info("System Time is " + now);
var gsdt = new GlideScheduleDateTime(now);
gsdt.setTimeZone("Europe/London");
gs.info("London time is " + gsdt.getGlideDateTime().getDisplayValue());

AnkurBawiskar_0-1752840871704.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Anjali Priya 
I have some Script Snips that will Help :-


// Step 1: Create GlideDateTime object with current CET time
var gdt = new GlideDateTime(); // current system time (UTC-based)

// Step 2: Get CET offset manually
var cetTZ = java.util.TimeZone.getTimeZone("CET");
var cetOffsetMillis = cetTZ.getOffset(new java.util.Date().getTime());

// Step 3: Add CET offset to UTC
var cetDate = new GlideDateTime();
cetDate.add(cetOffsetMillis / 1000); // converting ms to seconds

// Step 4: Format CET time
var cetJavaDate = cetDate.getDate().getJavaObject();

// Step 5: Convert to user-input timezone
var targetTZ = java.util.TimeZone.getTimeZone(targetTimeZone);
var sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(targetTZ);

var userTime = sdf.format(cetJavaDate);

gs.info("Converted CET time to " + targetTimeZone + ": " + userTime);
-----------------------------------
Note: These Script has been Validated and Working !!

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/