postman - Postman - Need help to store a variable from the date value(YYYY-MM-DD) in response by changing the date format to MMM-DD-YYYY
问题描述
I am getting my output in (YYYY-MM-DD). i want to save to an environment variable by changing the date format to MMM-DD-YYYY (Feb-26-2020)
Sample response:
{
"pickupDate": "2020-02-26",
"cutOffTime": "20:00:00",
"accessTime": {
"hours": 1,
"minutes": 0
},
}
Using below just to save the date as it is. But help me to change and save it.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("day1", jsonData.output.options[1].pickupDate);
解决方案
You should be able to do that in the Tests
tab of the Postman app using moment:
const moment = require('moment')
let jsonData = pm.response.json();
pm.environment.set("day1", moment(jsonData.output.options[1].pickupDate).format('MMM-DD-YYYY'));
You would need to bring in the module first and then use .format()
to change the date to what you want. I've also used the newer Postman syntax as you were still using the older syntax.
推荐阅读
- python - 如何使用 Selenium 和 Python 选择没有标题的元素?
- sql - 如果左连接表中的行为空,则使用另一行
- python - 是否可以使 login_window() 函数仅在单击/激活登录按钮时运行?
- regex - 如何只获得数字直到点?
- javascript - 反应解构状态以传递到组件道具
- c# - C# 不写入数据库
- flutter - 如何调用返回 Future 的异步函数
在三元函数中根据返回值有不同的颜色? - javascript - 我正在处理一个文档 config.env 并且在索引文档上调用它之后它没有工作
- macos - 如何让 Apple FaceTime ISP 摄像头报告当前曝光
- flowtype - 流类型定义被忽略以支持 npm 模块?