需要在预请求脚本中(Pre-requestScript),使用javaScript生成格式化时间,再加入postman的变量中
var moment = require('moment');
var date = moment().format("YYYY-MM-DD HH:mm:ss");
console.log(date);
pm.globals.set("time", date);
百度了半天,一堆在YY前面加了个空格的文章,导致接口一直提示转换失败。
var myDate=new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth() + 1;
var date = myDate.getDate();
var hours = myDate.getHours();
var min = myDate.getMinutes();
var seconds = myDate.getSeconds();
hours = hours < 10 ? "0" + hours : hours;
min = min < 10 ? "0" + min : min;
seconds = seconds < 10 ? "0" + seconds : seconds;
var date = year +"-"+ month +"-"+ date +" "+ hours +":"+ min +":"+seconds;
console.log(date);
pm.globals.set("time", date);
以上两种方式都可以,然后使用{{time}}来进行传值