Ste*_*ica 42 timestamp date postman
在Postman中,动态变量 {{$timestamp}}将当前的Unix时间戳插入请求中.(代表自1970年1月1日以来的秒数)
"currentTime": "1510934784"
Run Code Online (Sandbox Code Playgroud)
但是,我正在使用的API期望时间戳格式为MM/DD/YYYY.
"currentDate": "11/17/2017"
Run Code Online (Sandbox Code Playgroud)
如何将当前日期(格式化为MM/DD/YYYY)插入到Postman的请求中?
Dan*_*ton 67
您可以将post.js与Postman一起使用,为您提供时间戳格式.
您可以将其添加到预请求脚本:
const moment = require('moment');
pm.globals.set("timestamp", moment().format("MM/DD/YYYY"));
Run Code Online (Sandbox Code Playgroud)
然后参考{{timestamp}}你需要的地方.
有关Collection Level Pre-request Script在Postman中使用的更多信息,我写了一篇简短的博客文章:https://dannydainton.com/2018/05/21/hold-on-wait-a-moment/
Pay*_*jad 19
使用“预请求脚本”标签来编写JavaScript,以获取日期并将其保存到变量中:
const dateNow= new Date();
pm.environment.set('currentDate', dateNow.toISOString());
Run Code Online (Sandbox Code Playgroud)
然后在请求正文中使用它,如下所示:
"currentDate": "{{currentDate}}"
Run Code Online (Sandbox Code Playgroud)
Dea*_*ers 14
我的解决方案类似于 Payam 的,但我使用的是
//older code
//postman.setGlobalVariable("currentDate", new Date().toLocaleDateString());
pm.globals.set("currentDate", new Date().toLocaleDateString());
Run Code Online (Sandbox Code Playgroud)
如果您点击文件夹上的“3 个点”并单击“编辑”
然后为所有调用设置 Pre-Request Scripts,以便全局变量始终可用。
小智 5
JavaScript 中的任何未来日期(邮递员测试使用 JavaScript)都可以检索为:
var dateNow = new Date();
var twoWeeksFutureDate = new Date(dateNow.setDate(dateNow.getDate() + 14)).toISOString();
postman.setEnvironmentVariable("future-date", twoWeeksFutureDate);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56505 次 |
| 最近记录: |