Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 903
Stevoisiak Avatar asked Nov 17 '17 16:11

Stevoisiak


People also ask

How do I pass a timestamp in API?

Instead use a conditional GET request. This will also tell you that the timestamp to pass to the service will need to be in a format specified in Date and Time Specification section of RFC 2822. Show activity on this post. I like the idea of passing the number of seconds since 1970.

What is timestamp in Postman?

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"


2 Answers

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

You can add this to the pre-request script:

const moment = require('moment'); pm.globals.set("today", moment().format("MM/DD/YYYY")); 

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

If you add this to the Collection Level Pre-request Script, it will be run for each request in the Collection. Rather than needing to add it to all the requests individually.

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/

like image 63
Danny Dainton Avatar answered Oct 10 '22 06:10

Danny Dainton


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

const dateNow= new Date(); pm.environment.set('currentDate', dateNow.toISOString()); 

and then use it in the request body as follows:

"currentDate": "{{currentDate}}" 
like image 23
Payam Khaninejad Avatar answered Oct 10 '22 06:10

Payam Khaninejad