如何为邮递员请求生成未来的日期和时间

How to generate future date and time for postman request

{
    "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"
}

我卡在了需要生成带有时区的未来日期和时间以发送邮递员请求的步骤。该请求与预订功能有关。

在请求正文中,一个键值是,

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

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

提前致谢。

您可以在邮递员的 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}}"
}