how to convert glideDateTime that from GMT to EST using script include

ganesh7
Kilo Contributor

Hi All,

i want to write script include for converting GMT To EST, As

They are basically just Service Now's javascript class for working with date and time. They are a headache to use though, especially when it comes to time zones. Although the system time zone is set to Eastern Standard Time (EST), GlideDateTime objects are sometimes initiated in GMT (international time based out of London). To help with this we would like you to write the following script includes:

u_getLocTZ(gdt) -> takes GlideDateTime object and returns the object in the local time zone

u_getGMTTZ(gdt) —> takes GlideDateTime object and returns the object in the GMT time zone

u_isGMT(gdt) -> takes GlideDAteTime object and returns true if it is in the GMT time zone.

u_isLocTZ(gdt) -> takes GlideDateTime object and returns true if it is in the local time zone (EST).

could you please give some idea about this.

Regards,

Ganesh.

7 REPLIES 7

HV1
Mega Guru

You can use setTZ() method to set the object to a specific timezone.


Reference link: GlideDateTime - ServiceNow Wiki



Regards,


Hardik Vora


veeresh3
Tera Expert

var tz = gs.getSession().getTimeZone();


var gdt = new GlideDateTime();


gdt.setTZ(tz);



Thanks,


Veeresh


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Ganesh,



You mention that " ..Service Now's javascript class for working with date and time. They are a headache to use though, especially when it comes to time zones."


and "Although the system time zone is set to Eastern Standard Time (EST), GlideDateTime objects are sometimes initiated in GMT/UTC"



Working with date and time in combination with time zones is often a headache, agreed - especially if DST is involved.


But is that due to shortcomings in Service Now's javascript class for working with date and time, or because working with date and time is quite complex?



Info on a gdt can be fetched via the api which may give you the some useful info, in the context of your inquiry:


api described here:


GlideDateTime - ServiceNow Wiki


..


3.16 getDisplayValue()


Gets the date and time value in the current user's display format and time zone. Referring to the GlideDateTime object directly returns the date and time value in the GMT time zone.


..


3.18 getValue()


Gets the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and the system time zone, UTC by default.


..


3.70 getTZOffset()


Gets the time zone offset in milliseconds.


..



Also there is some best practice which could be applied, such as using Local time and UTC (gdt) methods:


..


Some methods use the Java Virtual Machine time zone when retrieving or modifying a date and time value. Using these methods may result in unexpected behavior. Use equivalent local time and UTC methods whenever possible. Local time and UTC methods are available starting with the Eureka release.


..



Best Regards



Tony






















ganesh7
Kilo Contributor

Tnx all,



the problem is




Service Now utilizes several different time zones. The system time zone in our instance is set to Eastern Standard Time (EST). However, GlideDateTime objects are often declared in GMT (global time based in London). There is also a third time object based on the Unix Time Stamp (seconds since 1 January 1970).


The problem is that it is not always clear which time zone an object is using. Even if you declare a GlideDateTime based on a field in the EST time zone, time is sometimes converted from the display time to GMT time.


For example:


ex: var gdt = new GlideDateTime();



We want to create a number of script includes to make using GlideDateTime objects a little easier:


1. u_getLocTZ(gdt) —> takes GlideDateTime object and returns the object in the local time zone


function u_getLocTZ(gdt){


// parameter: gdt — GlideDateTime object


/*


* Code to check if var gdt is already in EST.


* If not, convert to EST and return new value


*/


Return gdtLocTZ ;


}


2. u_getGMTTZ(gdt) -> takes GlideDateTime object and returns the object in the GMT time zone


Same as u_getLocTZ, except the other way around. Checks if var gdt is already in GMT, and if not, coverts it to GMT and returns the new value.


3. u_isGMT(gdt) —> takes GlideDateTime object. Returns true if it is in GMT time, false otherwise.


4. u_isLocTZ(gdt) —> takes GlideDateTime object. Returns true if it is in the local time zone, false otherwise



could you please help me to get like this.



Regards,


Ganesh.