如何在Postman中将{{$ timestamp}}格式化为MM / DD / YYYY?

时间:2017-11-17 16:27:25

标签: date timestamp postman

在Postman中,dynamic variable {{$timestamp}}会将当前Unix Time Stamp插入到请求中。 (代表自1970年1月1日以来的秒数)

"currentTime": "1510934784"

但是,我正在使用的API要求时间戳格式为MM/DD/YYYY

"currentDate": "11/17/2017"

如何将当前日期(格式为MM/DD/YYYY)插入到Postman的请求中?

5 个答案:

答案 0 :(得分:42)

您可以使用Postman的moment.js为您提供时间戳格式。

您可以将其添加到预请求脚本中:

var moment = require('moment')
pm.globals.set("timestamp", moment().format("MM/DD/YYYY"))

然后在您需要的地方引用{{timestamp}}

有关在Postman中使用moment的更多信息,我写了一篇简短的博文:https://dannydainton.com/2018/05/21/hold-on-wait-a-moment/

答案 1 :(得分:8)

我的解决方案与Payam的解决方案相似,除了我正在使用

const dateNow = new Date();
postman.setGlobalVariable("currentDate", dateNow.toLocaleDateString());

如果您点击文件夹上的“ 3点”,然后点击“编辑”

enter image description here

然后为所有调用设置预请求脚本,因此全局变量始终可用。

enter image description here

答案 2 :(得分:7)

使用“预请求脚本”标签来编写JavaScript,以获取日期并将其保存到变量中:

var dateNow= new Date();
postman.setEnvironmentVariable("currentDate", dateNow.toISOString());

,然后按如下所示在请求正文中使用它:

"currentDate": "{{currentDate}}"

答案 3 :(得分:5)

JavaScript中的任何将来日期(邮递员测试使用JavaScript)都可以检索为:

var dateNow = new Date();  
var twoWeeksFutureDate = new Date(dateNow.setDate(dateNow.getDate() + 14)).toISOString();

postman.setEnvironmentVariable("future-date", twoWeeksFutureDate);

答案 4 :(得分:0)

在PostMan中,我们有->预请求脚本。粘贴下面的代码段。

const dateNow = new Date();
postman.setGlobalVariable("todayDate", dateNow.toLocaleDateString());

现在我们可以使用了。

{
"firstName": "SANKAR",
"lastName": "B",
"email": "SANKARB@GMAIL.COM",
"creationDate": "{{todayDate}}"
}

如果您使用的是JPA实体类,请使用以下代码段

    @JsonFormat(pattern="MM/dd/yyyy")
    @Column(name = "creation_date")
    private Date creationDate;

enter image description here enter image description here