Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Convert epoch time to date/time in users time zone

Alamelu1
Tera Contributor

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.

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Chetan Mahajan
Mega Sage

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

Mario Quinones1
Mega Guru

@Alamelu , This is an original correct answer, give credit to the original author

 

https://community.servicenow.com/community?id=community_question&sys_id=22a45c2bdbbad7c0e0e80b55ca96...

Please mark helpful or correct if this works for you

Ankur Bawiskar
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

KishanGourav
Tera Expert


var epochTime = '1689076116';
var gdt = new GlideDateTime();
gdt.setNumericValue(epochTime * 1000);
gs.print('dt is ' + gdt.getValue());