Convert DateTime from one time zone to another in scoped App and Global

mahesh009
Tera Expert

how can we convert a time from one timezone to another timezone in both scoped applications and global?

1 ACCEPTED SOLUTION

mahesh009
Tera Expert
function convertDateTimeInGlobal(dateTimeStr, fromTimeZone, toTimeZone) {

    var gDate = new GlideDateTime();

    gDate.setTimeZone(fromTimeZone);

    gDate.setDisplayValue(dateTimeStr);

    gDate.setTimeZone(toTimeZone);

    return String(gDate.getDisplayValue());

}

gs.print(convertDateTimeInGlobal("2025-09-16 12:00:00", "GMT", "IST"));


function convertDateTimeInScopedAPP(dateTimeStr, fromTimeZone, toTimeZone) {

    var scheduleDateinfrTZ = new GlideScheduleDateTime("TZID=" + fromTimeZone + ";" + dateTimeStr);

    var dateTimeInternal = scheduleDateinfrTZ;

    var schedDateintoTz = new GlideScheduleDateTime(dateTimeInternal);

    schedDateintoTz.setTimeZone(toTimeZone);

    /* Return the converted date-time as a string */

    return String(schedDateintoTz);

}

gs.info(convertDateTimeInScopedAPP("2025-09-16 12:00:00", "GMT", "IST"));

View solution in original post

4 REPLIES 4

mahesh009
Tera Expert
function convertDateTimeInGlobal(dateTimeStr, fromTimeZone, toTimeZone) {

    var gDate = new GlideDateTime();

    gDate.setTimeZone(fromTimeZone);

    gDate.setDisplayValue(dateTimeStr);

    gDate.setTimeZone(toTimeZone);

    return String(gDate.getDisplayValue());

}

gs.print(convertDateTimeInGlobal("2025-09-16 12:00:00", "GMT", "IST"));


function convertDateTimeInScopedAPP(dateTimeStr, fromTimeZone, toTimeZone) {

    var scheduleDateinfrTZ = new GlideScheduleDateTime("TZID=" + fromTimeZone + ";" + dateTimeStr);

    var dateTimeInternal = scheduleDateinfrTZ;

    var schedDateintoTz = new GlideScheduleDateTime(dateTimeInternal);

    schedDateintoTz.setTimeZone(toTimeZone);

    /* Return the converted date-time as a string */

    return String(schedDateintoTz);

}

gs.info(convertDateTimeInScopedAPP("2025-09-16 12:00:00", "GMT", "IST"));

@mahesh009 

I also shared a working solution.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@mahesh009 

this will work in both

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());

Global Output

AnkurBawiskar_0-1758036818484.png

Scoped Output

AnkurBawiskar_1-1758036853223.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

While this solution converts time from the system's local time zone to another, my approach offers greater flexibility by enabling conversions between any two time zones, regardless of the system timezone