如何为 Postman 请求生成未来的日期和时间

jgwigjjp  于 12个月前  发布在  Postman
关注(0)|答案(2)|浏览(175)
{
    "name": "IronMan",
    "phone_number": "555555555",
    "number_of_guest": 10,
    "tables": [
        2
    ],
    "reservation_start_at": "2020-10-15T10:00:00.861873Z",
    "reservation_end": "2020-10-15T11:00:00.861873Z"
}

字符串
我卡在步骤中,我需要生成未来的日期和时间与时区发送 Postman 请求。该请求是关于预订功能。
在请求体上,键值是,

"reservation_start_at": "2020-10-15T10:00:00.861873Z"


我的问题是,如何为每个请求生成未来的日期和时间?
先谢了。

kninwzqo

kninwzqo1#

您可以在postman的pre-request脚本中使用以下内容:

var moment = require('moment');

const yearsInTheFuture = 1;
const monthsInTheFuture = 2;
const daysInTheFuture = 3;
const hours = 12;
const minutes = 34;
const seconds = 56;
const milliseconds = 789;
const durationInDays = 7;

const reservation_start_at = moment()
    .add(yearsInTheFuture, 'years')
    .add(monthsInTheFuture, 'months')
    .add(daysInTheFuture, 'days')
    .hours(hours)
    .minutes(minutes)
    .seconds(seconds)
    .millisecond(milliseconds);

reservation_end = moment()
    .add(yearsInTheFuture, 'years')
    .add(monthsInTheFuture, 'months')
    .add(daysInTheFuture + durationInDays, 'days')
    .hours(hours)
    .minutes(minutes)
    .seconds(seconds)
    .millisecond(milliseconds);

console.log("reservation_start_at: " + reservation_start_at);
console.log("reservation_end: " + reservation_end);

pm.environment.set("reservation_start_at ", reservation_start_at);
pm.environment.set("reservation_end", reservation_end);

字符串
它将设置环境变量reservation_start_atreservation_end,然后您可以在请求主体中访问它们:

{
   "name":"IronMan",
   "phone_number":"555555555",
   "number_of_guest":10,
   "tables":[
      2
   ],
   "reservation_start_at":"{{reservation_start_at}}",
   "reservation_end":"{{reservation_end}}"
}

oxcyiej7

oxcyiej72#

请尝试以下脚本生成今天的日期+ 10天

var moment = require('moment');
    pm.environment.set("futuredate", moment().add(10, 'day').format("MM/DD/YYYY"));

字符串

相关问题