Simple way to convert Server Date/Time format into Client Date/Time?

georgechen
Kilo Guru

Hi folks,

I am trying to convert a server date/time format into a client date/time.       There are two ways I can achieve this, one is to compensate the 10 hours difference (we are in AEST 10+ zone) to the date/time on server, GlideDateTime.addSeconds (60 seconds * 60 minutes * 10 hours)

The other I believe it can be achieved by GlideDateTime getLocalTime and getLocalDate functions, according to GlideDateTime Wiki, http://wiki.servicenow.com/index.php?title=GlideDateTime#getLocalDate.28.29 , the GlideDateTime.getLocalDate() and GlideDateTime.getLocalTime() can return the date/time in user's time zone.

However, not so sure if there is a direct function that can convert both date and time.     For example,

var grIN = new GlideRecord('incident');

grIN.get('7a98b7dc4f580300052ea40f0310c762');     //query a particular IN record

gs.print(grIN.sys_created_on);   // this returns *** Script: 2017-07-28 01:14:40 , which is server time

var d1 = new GlideDateTime(grIN.sys_created_on);

gs.print('Local Date: ' + d1.getLocalDate()); // this returns *** Script: Local Date: 2017-07-28

gs.print('Local Date Time: ' + d1.toString()); // this returns *** Script: Local Date Time: 2017-07-28 01:14:40

gs.print(d1.getLocalTime()); // this returns *** Script: 1970-01-01 11:14:40, which returns correct local time, but invalid date.

Is there a function that can convert both date and time?

Thank you in advance.

1 ACCEPTED SOLUTION

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi George,



The easiest way is just to return the display value which is in the user timezone:



var gr = new GlideRecord('incident');


gr.get('ef43c6d40a0a0b5700c77f9bf387afe3');



gs.print(gr.sys_created_on); //GMT


gs.print(gr.sys_created_on.getDisplayValue());     //PST



I get:



[0:00:00.004] Script completed in scope global: script


*** Script: 2017-03-07 18:05:40     //GMT


*** Script: 2017-03-07 10:05:40     //PST


View solution in original post

2 REPLIES 2

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi George,



The easiest way is just to return the display value which is in the user timezone:



var gr = new GlideRecord('incident');


gr.get('ef43c6d40a0a0b5700c77f9bf387afe3');



gs.print(gr.sys_created_on); //GMT


gs.print(gr.sys_created_on.getDisplayValue());     //PST



I get:



[0:00:00.004] Script completed in scope global: script


*** Script: 2017-03-07 18:05:40     //GMT


*** Script: 2017-03-07 10:05:40     //PST


Thanks Sergiu.   didn't realize it would be that simple.     Appreciate your advice.


Kind regards,


George