How do I format {{$timestamp}} as MM/DD/YYYY in Po

2019-02-06 04:00发布

问题:

In Postman, the dynamic variable {{$timestamp}} inserts the current Unix Time Stamp into a request. (Represented as the number of seconds since January 1, 1970)

"currentTime": "1510934784"

However, the API I am working with expects timestamps formatted as MM/DD/YYYY.

"currentDate": "11/17/2017"

How do I insert the current date (formatted as MM/DD/YYYY) into my request with Postman?

回答1:

You could use moment.js with Postman to give you that timestamp format.

You can add this to the pre-request script:

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

Then reference {{timestamp}} where ever you need it.

For more information about using moment in Postman, I wrote a short blog post: https://dannydainton.com/2018/05/21/hold-on-wait-a-moment/



回答2:

Use Pre-request script tab to write javascript to get and save the date into a variable:

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

and then use it in the request body as follows:

"currentDate": "{{currentDate}}"