Ankur Bawiskar
Tera Patron
Tera Patron

Many a times there is a need to convert date/time to particular timezone.

Example: If we are using email notifications we might require to send the Opened/Created date in EST or IST timezone.

There is no direct way to convert it without scripting.

Here is a sample script which would help converting the date/time to required timezone. You can use this in your email script or any server side scripts

In the below script I am converting the GMT time to IST time.

Script:

var time = new GlideDateTime();

gs.info('GMT time is->' + time);

var targetTimezone = 'IST'; // ensure you give correct timezone Abbreviation

var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone); 

time.setTZ(tz);

var timeZoneOffSet = time.getTZOffset();

time.setNumericValue(time.getNumericValue() + timeZoneOffSet);

gs.info('IST time is->' + time);

Output:

find_real_file.png

Note: This script involved Package calls which won't work in scoped application. For scoped application please refer below links which might help you which uses moment.js

OR you can create script include in Global scope and make it accessible from all scopes and then invoke the script

Scoped application - How to get current date/time of a specific timezone.

Workflow wait for condition based on variable date/Time and Timezone

Live Coding Happy Hour for 2017-02-17 - Time Math with Moment.js

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.
Thanks,
Ankur Bawiskar

ServiceNow MVP 2021,2020,2019,2018

Developer MVP 2021

My Articles & Blogs

14 Comments