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

divyasurada
Tera Contributor

Hi,

I am trying to get the time difference between the current time/Date and end_time (time/date) based on Time zone provided by the user in SCOPED application.

To achieve this I need to first get the current time/date in the time zone provided by the user. Please help me with this.

Thanks in Advance.

 

1 ACCEPTED SOLUTION

Anirban Roy
Mega Guru

Hi,

You would need to add a timezone offset to get the date time in a specific timezone.

This can be achieved in global scope using package calls or using setTZ method for a logged in users session object.

However, there is no api accessible in scoped apps. You may look at the moment.js opensource library.

//below piece of code will give you the timezone offset w.r.t UTC...

var tZSetOnVarOffSet = moment.tz(moment.utc(), tZSetOnVar).utcOffset();

//-- you can add the timezone offset to your current date time to fetch the date time in another timezone

I am adding the link of your previous question as well, as the solution for both are similar. You can follow the detailed script in the below question answer -

https://community.servicenow.com/community?id=community_question&sys_id=871e4912dbfd90105d782183ca96...

 

Regards,

Anirban

View solution in original post

6 REPLIES 6

Gowrisankar Sat
Tera Guru

Hi Divya,

This is for current time:

var tz = "<Timezone value> i.e.Canada/Eastern";

//var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");

var gDT = new GlideDateTime();

gDT.setTZ(tz);

var abc = gDT.getDisplayValue();

 

For a field time use:

var tz = "<Timezone value> i.e.Canada/Eastern";

var gDT = new GlideDateTime(current.<field_name>);

gDT.setTZ(tz);

var abc = gDT.getDisplayValue();

Check this link for reference: https://community.servicenow.com/community?id=community_question&sys_id=4714cfe5dbd8dbc01dcaf3231f96...

This method does not work as this is a scoped application

Gowrisankar Sat
Tera Guru

Anirban Roy
Mega Guru

Hi,

You would need to add a timezone offset to get the date time in a specific timezone.

This can be achieved in global scope using package calls or using setTZ method for a logged in users session object.

However, there is no api accessible in scoped apps. You may look at the moment.js opensource library.

//below piece of code will give you the timezone offset w.r.t UTC...

var tZSetOnVarOffSet = moment.tz(moment.utc(), tZSetOnVar).utcOffset();

//-- you can add the timezone offset to your current date time to fetch the date time in another timezone

I am adding the link of your previous question as well, as the solution for both are similar. You can follow the detailed script in the below question answer -

https://community.servicenow.com/community?id=community_question&sys_id=871e4912dbfd90105d782183ca96...

 

Regards,

Anirban