Convert GlideTime to local time in a transform map

David77
Giga Guru

We're trying to populate a date field with local time (vs GMT) in a date field, with something like

 

var gd = new GlideDate(); // this returns GMT

gd = gd.getByFormat('yyyy-MM-dd HH:mm:ss'); // convert to ServiceNow date format

gd = gd.setDisplayValue(); // convert to local time

return gd;

 

Without the call to setDisplayValue, I get GMT time, but when I add that line, it fails in the transform. 

What am i doing wrong?

Thanks!

4 REPLIES 4

Prasad Dhumal
Mega Sage
Mega Sage

Hello David,

use setDisplayValue() to set the GlideDate object to the local time, and then call getDisplayValue() to retrieve the local time in the desired format.

 

var gd = new GlideDate(); // this returns GMT
gd = gd.getByFormat('yyyy-MM-dd HH:mm:ss'); // convert to ServiceNow date format
gd.setDisplayValue(); // convert to local time
var localTime = gd.getDisplayValue('yyyy-MM-dd HH:mm:ss'); // retrieve local time in desired format
return localTime;

 

 

Thanks for the quick response, but that code is not working for me ... 

Sorry to hear that,

Could you please try below updated script?

var gd = new GlideDate();
gd = gd.getByFormat('yyyy-MM-dd HH:mm:ss');
var localTime = new GlideDate();
localTime.setValue(gd.getValue());
return localTime.getDisplayValue('yyyy-MM-dd HH:mm:ss');

DrewW
Mega Sage
Mega Sage

In ServiceNow Date/Time fields are always in GMT in the background and then they display in the users set time zone when viewed.

If its a transform map then just set the date format field to the right value assuming the date/times are in the system time zone.

If the values in your import are all in a specific time zone that is not the system time zone then you can do something like this for say the EST time zone

gd.setDisplayValue(source.getValue("DATE_TIME_FIELD") + " EST", "yyyy-MM-dd HH:mm:ss zzz");

 

and then return gd and you should get your date/time field set to the correct GMT value so that when you look at it for the time zone you are in.  In the case I did above when I set my time zone to EST I should see the same value in the import file.