How to convert 'epoch' time to GMT time and populate on a date field?

Varun Sai
Tera Contributor

I am trying to convert 'epoch' time to GMT time and populate the field. 

The field is populated through an integration in a staging table 'Created at' field, and needs to be populated on the incident table field - u_incident_field.

any help is appreciated. 

 

1 ACCEPTED SOLUTION

Hi, '2022-07-21T14:25:56.871Z' is not Epoch time format
https://en.wikipedia.org/wiki/Unix_time

If the time is in UTC and as long as you are happy to ignore fractions of second and always round down to nearest second you can simply use string methods to cut and dice it into a format that GideDateTime will understand.

https://www.w3schools.com/jsref/jsref_obj_string.asp
https://www.w3schools.com/js/js_string_methods.asp

If using scoped GlideDateTime, you could also try replacing T with 'T' as the documentation indicates that it accepts this format - Note: I have never tried this.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideDateTimeAPI#r_GDT-GlideDateTime

If this is part of an integration\transform process then the 'string' results should be all you need to map directly to your target fields

var sourceTime = '2022-07-21T14:25:56.871Z';

var myTime = sourceTime.substring(0,10) + ' ' + sourceTime.substring(11,19);
var myTime2 = sourceTime.substring(0, sourceTime.indexOf('.')).replace('T', ' ');
var myTime3 = sourceTime.replace("T", "'T'");

gs.info(myTime);
gs.info(myTime2);
gs.info(myTime3);

//If you need to instantiate a GlideDateTime object, the this should be the syntax 
var gdt = new GlideDateTime(myTime);

gs.info(gdt);
gs.info(typeof(gdt));
gs.info(gdt.getDisplayValue());

 

 

 

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, unfortunately your images are not showing, I think they need to be attached as files rather than pasted inline as the community is currently having issues.

To set UTC in Global scope you can simply use setNumericvalue()
GlideDateTime | ServiceNow Developers


For a scoped app you can use addSeconds()
GlideDateTime | ServiceNow Developers

You may need to multiple or divide your Epoch value by 1000 depending on the solution, and where the Epoch value is supplied as seconds\milliseconds.

var epoch1 = 1658456400;
var epoch2 = 1658456400000;

var gdt = new GlideDateTime();
gdt.setNumericValue(epoch1 * 1000);
gs.info('Global scope ' + gdt);

var gdt2 = new GlideDateTime('1970-01-01 00:00:00');
gdt2.addSeconds(epoch2 / 1000);
gs.info('Scoped App ' + gdt2);

Hi @Tony Chatfield,

Thank you for your response.

Sorry, I should have seen that the screenshots were not attached properly. 

I am trying to get the 'created at' field on the alert table which has the value '2022-07-21T14:25:56.871Z' to autopopulate on the security incident table field 'u_incident_occured' which is of type 'glide_date_time'. 

The time value is in UTC and I just need to split it from '2022-07-21T14:25:56.871Z' to ' like this '2022-07-21 14:25:56'

I have written the below code, and I am getting an error.

var gdt = new GlideDateTime();
      gdt.getNumericValue(created_at);
      var date = gdt.split('T')[0];
      var time = gdt.split('T')[1].split('.')[0];
      var dateTime = date + ' ' + time;

 

 

 

Hi, '2022-07-21T14:25:56.871Z' is not Epoch time format
https://en.wikipedia.org/wiki/Unix_time

If the time is in UTC and as long as you are happy to ignore fractions of second and always round down to nearest second you can simply use string methods to cut and dice it into a format that GideDateTime will understand.

https://www.w3schools.com/jsref/jsref_obj_string.asp
https://www.w3schools.com/js/js_string_methods.asp

If using scoped GlideDateTime, you could also try replacing T with 'T' as the documentation indicates that it accepts this format - Note: I have never tried this.

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideDateTimeAPI#r_GDT-GlideDateTime

If this is part of an integration\transform process then the 'string' results should be all you need to map directly to your target fields

var sourceTime = '2022-07-21T14:25:56.871Z';

var myTime = sourceTime.substring(0,10) + ' ' + sourceTime.substring(11,19);
var myTime2 = sourceTime.substring(0, sourceTime.indexOf('.')).replace('T', ' ');
var myTime3 = sourceTime.replace("T", "'T'");

gs.info(myTime);
gs.info(myTime2);
gs.info(myTime3);

//If you need to instantiate a GlideDateTime object, the this should be the syntax 
var gdt = new GlideDateTime(myTime);

gs.info(gdt);
gs.info(typeof(gdt));
gs.info(gdt.getDisplayValue());