Convert GlideTime to local time in a transform map

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2023 11:32 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2023 11:39 AM
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2023 01:28 PM
Thanks for the quick response, but that code is not working for me ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 07:42 AM
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');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2023 11:45 AM - edited 02-15-2023 11:49 AM
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.