The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Epoc Date conversion

monu1
Giga Contributor

Hello Everyone,

In our project , ticket is created through Integration with Remedy.

So they are sending Planned Start And End Date of Change in EPOC format.

I have converted in Servicenow in yyyy/mm/dd HH:mm:ss format. But the time is not working Perfectly

 

Code:

var timestamp = 1536914778;

var dt = new Date(timestamp*1000);
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var day = dt.getDate();
var hours = dt.getHours();
var minutes = dt.getMinutes();
var seconds = dt.getSeconds();

document.write(year+"-"+month+"-"+day+" " +hours+":"+minutes+":"+seconds);

 

Output: 

2018-9-14 1:46:18(servicenow)
2018-9-14 14:16:18(javascript console)

It should come as Javascript console but in servicenow the timeing is coming as 1 instead of 14.

Could anyone tell me why the time is not working correctly in Servicenow.
1 ACCEPTED SOLUTION

johnnyd54
Giga Expert

Epoch is just the amount of time that has passed since a certain point in time. Service-Now, Unix, and many others you'll see use Jan 1, 1970 as the point of time. So to get the amount of ms from that point of time, use this:

 

var epoch = new GlideDateTime().getNumericValue();

 

This gives you the value in ms..

 

Now if you take that value and put it in

var gdt = new GlideDateTime();
gdt.setNumericValue(epoch);
gs.print(gdt);

It will give you the current date/time in UTC time.

 

Local time you can use gs.print(gdt.getDisplayValue());

 

View solution in original post

5 REPLIES 5

johnnyd54
Giga Expert

Epoch is just the amount of time that has passed since a certain point in time. Service-Now, Unix, and many others you'll see use Jan 1, 1970 as the point of time. So to get the amount of ms from that point of time, use this:

 

var epoch = new GlideDateTime().getNumericValue();

 

This gives you the value in ms..

 

Now if you take that value and put it in

var gdt = new GlideDateTime();
gdt.setNumericValue(epoch);
gs.print(gdt);

It will give you the current date/time in UTC time.

 

Local time you can use gs.print(gdt.getDisplayValue());