提问人:catnap 提问时间:10/31/2023 最后编辑:catnap 更新时间:11/6/2023 访问量:33
邮递员将 unix 时间(以微秒为单位)更改为日期时间
Postman change unix time in microseconds to datetime
问:
我有一个JSON响应,其中包括以微秒为单位的unix时间戳。我想要: 一个。在响应中添加一个字段,使其显示日期+时间;或 b.将字段本身更改为日期+时间。
以较容易者为准,但最好是 a。我需要的格式是 DD/MM/YYYY HH:MM:SS.sssssss
我正在查看以下线程,但我并不真正理解它们:
https://community.postman.com/t/convert-datetime-format-of-response-from-epoch-to-datetime/35320
https://learning.postman.com/docs/postman-flows/reference/working-with-date-and-time/
我试着查看 FQL 文档,但这也太深入了。
这是示例响应的一部分:
"SequenceNumber": 230901153510049870,
"Trade": {
"LastPrice": 73.6,
"LastSize": 53798,
"Timestamp": 1693582510115510,
我希望它看起来像这样:
"SequenceNumber": 230901153510049870,
"Trade": {
"LastPrice": 73.6,
"LastSize": 53798,
"Timestamp": 1693582510115510,
** "DateTime": 01/09/2023 15:35:10.115510,**
完整示例输出:
{
"ErrorCode": 0,
"Outcome": "Success",
"Ticks": [
{
"SequenceNumber": 230814040000019593,
"IsExtendedHour": false,
"Ask": null,
"Bid": null,
"Trade": {
"LastPrice": 74700.0,
"LastSize": 100,
"Timestamp": 1691985600000000,
"OptionalFields": {
"TradeId": "58850000",
"size": "100",
"price": "74700",
"style": "-1",
"type": "0",
"minorcurrencytype": "-1",
"currency": "964",
"marketsession": "0"
}
}
},
{
"SequenceNumber": 230814040000019591,
"IsExtendedHour": false,
"Ask": null,
"Bid": null,
"Trade": {
"LastPrice": 74700.0,
"LastSize": 200,
"Timestamp": 1691985600000000,
"OptionalFields": {
"TradeId": "58840000",
"size": "200",
"price": "74700",
"style": "-1",
"type": "0",
"minorcurrencytype": "-1",
"currency": "964",
"marketsession": "0"
}
}
}
],
"DepthOfBooks": []
}
答:
0赞
mikee
11/6/2023
#1
在以下示例中,我将遍历已解析的响应,在可视化工具中使用时间戳之前更新时间戳。
我必须对时间戳进行切片,以使其按照您的要求显示。(我仍然不确定最后六位数字是什么)。
const response = pm.response.json();
var moment = require('moment');
response.Ticks.forEach(obj => {
let utcMicroseconds = obj.Trade.Timestamp.toString();
let utcSeconds = parseInt(utcMicroseconds.slice(0, 10)) * 1000;
let remainder = parseInt(utcMicroseconds.slice(-6));
let d = moment(utcSeconds).format("DD/MM/YYYY hh:mm:ss");
obj.Trade.Timestamp = d + "." + remainder;
});
var template = `
<style type="text/css">
.tftable {font-size:14px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
.tftable th {font-size:18px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;position:sticky;top:0;}
.tftable tr {background-color:#ffffff;}
.tftable td {font-size:14px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
.tftable tr:hover {background-color:#e0ffff;}
</style>
<table class="tftable" border="1">
<tr>
<th>Sequence Number</th>
<th>Timestamp</th>
</tr>
{{#each response}}
<tr>
<td>{{SequenceNumber}}</td>
<td>{{Trade.Timestamp}}</td>
</tr>
{{/each}}
</table>
`;
pm.visualizer.set(template, {
response: response.Ticks
});
这将在可视化工具中生成以下内容。(请注意,时间显示的是我的时区,所以它有一个小时)。
评论