Convert readable time to unix / epoch time in call body?

I need to call an API that expects unix timestamps / epoch time values, so I have to use a separate site or tool to convert human-readable values to that format – and then if the call doesn’t return the results I’d expect, then while re-checking all the parameters I have to convert them back to human-readable to make sure the values are all really what I want them to be. Awkward.

Is there any way I can use human-readable timestamps in the call body in a way that they would get converted when the call is placed?

Any of these would be preferable to unix for the human-readable format:
YYYYMMDDHHMMSS
YYYYMMDD HHMMSS
YYYY-MM-DD HH:MM:SS ← favorite

Hey @josh999 :wave:

Welcome to the Postman Community! :postman:

Do you have an example of the timestamp that you want to convert?

In the script sandbox, you can use moment which can be used to format a timestamp.

Here’s a rough example:

let moment = require('moment');
console.log(moment(1711126960).format('YYYY-MM-DD HH:MM:SS'));

If you need to use that elsewhere in your requests, this could be done in a pre-request script and saved as a variable.

Essentially I’ll be using timestamps to specify a range from which I want records retrieved, so it could be relative (from an hour ago to now) but it could also be any day/time in the past from which records are of interest at the moment – so really, any time stamp.

Example 2024-03-20 13:02:00

My hope is that there’s a way to include that in the call body somewhere (in the format where I can understand what time value I’m looking at without translation) but it can be converted to unix time format when the call is placed since the system I’m calling requires unix time.

What does the current request look like? What’s the format of the request body?

You can create what you need in a pre request script, set that as a variable and then use that value within the request body/url/params/some other part of the request.

I can help you craft this but as I don’t know your context and what you’re currently using it’s tricky to know what to suggest. :sweat_smile:

To pull back all records saved at or since a specific time, the body of the call can be as simple as:

{“min_save”:“2024-03-20 13:02:00”}

…except that what the calling system is expecting is:

{“min_save”:“1710957720”}

So you need something that you enter your human friendly time and convert that into that Unix timestamp all before sending the request?

Yeah, I know I can do it via separate tools but would like to have it right there in postman (ideally in a spot where I can see the human-readable timestamp in relation to the other parameters while verifying that my request is good to go, as there are a variety of other parameters that can be included in the request).

Here’s something basic that you could use or adapt to your needs.

Add this to the request body:

// using the variable created in the pre-request script
{
    "min_save": "{{convertedTimestamp}}"
}

Add this to the pre-request script:

const moment = require("moment");
// the value is hardcoded in the script by this can be made a variable and change in a single place
let friendlyTime = "2024-03-20 13: 02:00";
// converted the human friendly time into a unix timestamp
let convertedTime = moment(friendlyTime).unix();
// using a runtime only variable so it's not stored outside the request execution 
pm.variables.set("convertedTimestamp", convertedTime)

In the script, it’s taking the human friendly time and using the .unix() function in the moment lib to convert it. It’s storing that as a temp runtime only variable so it can be used in the request body.

The example request is sent and in the response, you can see that the unix timestamp is being sent in the request body.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.