Epoch to DateTime JSON Variable Conversion

Hello Apigee Community,

What's the best way to convert a JSON variable from EPOCH to DateTime?

Can this be done via JavaScript Policy?

If so, do you have an example?

Thank you!

Solved Solved
0 4 5,884
1 ACCEPTED SOLUTION

This ended up working for me:

var dateTime = JSON.parse(context.getVariable('PaymentDateTime'));
var PaymentDateTime = new Date(dateTime * 1000).toISOString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'");
context.setVariable("PaymentDateTime", PaymentDateTime);

View solution in original post

4 REPLIES 4

Not sure what you mean by json variable but you can use below js snippet to convert to datetime.

var timestamp = context.getVariable("system.timestamp");
var mydate = new Date(timestamp);
var isoDate = mydate.toString('dddd MMM yyyy h:mm:ss');
context.setVariable("isoDate", isoDate);

If I want to replace "PaymentDateTime" to DateTime how would I do this?

Also, if you have any inputs on how I can convert the "PaymentAmount" to the "70.00" that would be helpful as well.

Thank you!

{ 

"PaymentAmount": "7000",
"PaymentDateTime": "1523971980" 

}

This ended up working for me:

var dateTime = JSON.parse(context.getVariable('PaymentDateTime'));
var PaymentDateTime = new Date(dateTime * 1000).toISOString("yyyy'-'MM'-'dd HH':'mm':'ss'Z'");
context.setVariable("PaymentDateTime", PaymentDateTime);

Glad you are able to resolve.