Search code examples
datetimestamppostman

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?


Solution

  • 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/