Time conversion between different timezones in scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2025 04:08 AM - edited 07-18-2025 04:44 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2025 04:51 AM - edited 07-18-2025 06:40 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2025 05:14 AM
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());
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2025 05:25 AM
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/