Convert date time to another timezone

John Doe
Giga Expert

To be specific, I'm working in scoped application Qualys one, there I need to convert timezone, it seems it is not working there using Javapackages, can anyone help? Also I am trying to debug using fix script but it is not printing any output

 

I tried this thing also

var time = new GlideDateTime();     // Display date retrieved       
gs.print('GMT Time: '+time);     // Set timezone     
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York"); 
//var tz = Packages.java.util.TimeZone.getTimeZone("IST");
 time.setTZ(tz);     // Get offset of timezone set above     
var timeZoneOffSet = time.getTZOffset();     // Add offset to current time     
 time.setNumericValue(time.getNumericValue() + timeZoneOffSet);     // Print date/time in desired timezone     
gs.print('Eastern time: '+time);

 

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hey,

Package calls aren't allowed in scoped applications. If you want to change the Timezone of a GlideDateTime object you can use the following.

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

find_real_file.png

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

check script shared by Kieran

regards
Ankur

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

@John Doe 

this works well in scoped app as well

var dateTime = new GlideDateTime();

var sourceTimeZone = 'GMT';
var targetTimeZone = 'IST';

var finalValue = global.rmConvertTimeZone(dateTime, sourceTimeZone, targetTimeZone);

gs.info(finalValue); 

[0:00:00.063] Script completed in scope x_421457_testing: script


Script execution history and recovery available here


x_421457_testing: 2022-09-05 10:25:27

Regards
Ankur

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

@John Doe 

Did you check script I shared? It works well

Somehow I ran the script shared by Kieran and it seems it's not showing the correct output

I used Europe/Berlin and Berlin is 2hours ahead of GMT but it shows only 1 hour ahead

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

[0:00:00.106] Script completed in scope x_421457_testing: script


Script execution history and recovery available here


x_421457_testing: System Time is 2022-09-05 05:56:34
x_421457_testing: Berlin time is 2022-09-05 04:56:34

Regards
Ankur

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

Is your system timezone set to to UTC? If it's not, and say set to Europe/London, you're going to get an incorrect time displacement

shiva_reddy_123
Tera Contributor

@Ankur Bawiskar 

how to convert date and time of selected timezone to UTC irrespective of user's time zone and system's time zone.

Note: I am using Script include with two inputs from client script 

input1 : date and time selected by user( any future date as well).

input2 : Time zone 

 

Expected output from my script to get Date and time in UTC

 

Regards

Shiva Reddy