- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2021 03:19 AM
The incoming payload to a transform script, has an attribute that is epoch time. I want to convert this to human readable date/time in users time zone and assign it to a field in the change record.How do I do it?
I tried the below,
var session = gs.getSession();
var tz = session.getTimeZoneName();
gs.info("User's Timezone: " + tz);
var now = new Date(timestamp); // here timestamp has the numeric epoch time from incoming payload
gs.info(now);
year = "" + now.getFullYear();
month = "" + (now.getMonth() + 1);
if (month.length == 1) {
month = "0" + month;
}
day = "" + now.getDate();
if (day.length == 1) {
day = "0" + day;
}
hour = "" + now.getHours();
if (hour.length == 1) {
hour = "0" + hour;
}
minute = "" + now.getMinutes();
if (minute.length == 1) {
minute = "0" + minute;
}
second = "" + now.getSeconds();
if (second.length == 1) {
second = "0" + second;
}
var starttime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
gs.info("Start Time :" + starttime);
var userTime = new Date(timestamp).toLocaleString("en-US", {
timeZone: tz
});
userTime = new Date(userTime);
gs.info('Start Time in users timezone: ' + userTime.toLocaleString());
This was populating the field in change record with time in PST and not the user’s timezone.
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2021 03:27 AM
Hi,
you can update the script as this
var epochTime = parseInt(source.u_time);
var gdt = new GlideDateTime();
gdt.setNumericValue(epochTime);
target.setValue('targetField', gdt);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2021 03:24 AM
Hello Alamelu,
Please try using GlideDateTime() as below
var epochTime = '1578912558000';
var gdt = new GlideDateTime();
gdt.setNumericValue(parseInt(epochTime));
gs.info('dt is ' + gdt.getDisplayValue()); // checking
field.setvalue('Field_Name',gdt);
please mark correct/Helpful if you get your solution
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2021 03:24 AM
Please mark helpful or correct if this works for you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2021 03:27 AM
Hi,
you can update the script as this
var epochTime = parseInt(source.u_time);
var gdt = new GlideDateTime();
gdt.setNumericValue(epochTime);
target.setValue('targetField', gdt);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2023 05:57 AM
var epochTime = '1689076116';
var gdt = new GlideDateTime();
gdt.setNumericValue(epochTime * 1000);
gs.print('dt is ' + gdt.getValue());