Getting Current Time for different Timezones

deepak50
Tera Contributor

Hi,

How Can I get the Current time for different timezones in script include.

For eg Current time in EST/PST/US Mountain etc. User can have any timezone setting in Profile like EST or PST but script should always give current time based on Timezone name passed.

I am working in scoped app and using below api

 

GlideScheduleDateTime(gdt).convertTimeZone(Timezone1 , TimeZone2);

 

But this is giving correct result only if user has Timezone setting in profile as GMT , It is failing is user has timezone setting as EST or PST in profile.

 

Thanks

Deepak

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

Hi,

If you are working in Global scope, then you have the option of using

gs.nowDateTime();

which returns the time in the current users time-zone setting.

Community Alums
Not applicable

Hi @deepak50 ,

Certainly! If you want to generate the current date in UTC using GlideDateTime in ServiceNow, you can follow this code snippet:

 

gs.info(new GlideDateTime());

 

The following code will return the current date/time in the specific timezone you want:

 

vargt =newGlideDateTime();
gt.setTimeZone('PST');//["US/Eastern" , "US/Eastern" , "PST" ]
gs.info(gt.getDisplayValue());

 

You can utilize the above code in server-side scripting.

 

If this helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.

 

regards,

Prasad

Hi Prasad, I am working in app scope , so setTimeZone is giving error:

problem: java.lang.SecurityException: Function setTimeZone is not allowed in scope 

Thanks

Deepak

 

Community Alums
Not applicable

@deepak50 , Didn't found any solution yet. But could you please try making your application cross-scoped by providing cross-scope privileges?

 

-Prasad