0
0
Postmantesting~5 mins

Timestamp generation in Postman

Choose your learning style9 modes available
Introduction

Timestamps help record the exact time an event happens. They are useful to track when tests run or when data is created.

You want to log the time a test request was sent.
You need to create unique data with the current time.
You want to compare times between different test steps.
You want to add a time-based parameter to an API call.
You want to check if a response time is within limits.
Syntax
Postman
var timestamp = Date.now();

Date.now() returns the current time in milliseconds since January 1, 1970 (called Unix epoch).

You can use this timestamp in your Postman scripts to mark the current time.

Examples
This gets the current timestamp and prints it in the Postman console.
Postman
var timestamp = Date.now();
console.log(timestamp);
This saves the current timestamp into an environment variable for later use.
Postman
pm.environment.set('currentTimestamp', Date.now());
This converts the timestamp to seconds by dividing by 1000 and rounding down.
Postman
var timestampSeconds = Math.floor(Date.now() / 1000);
console.log(timestampSeconds);
Sample Program

This test generates a timestamp, saves it to an environment variable, and checks that it is a positive number. It also prints the timestamp to the console.

Postman
pm.test('Timestamp is generated and saved', () => {
    const timestamp = Date.now();
    pm.environment.set('testTimestamp', timestamp);
    pm.expect(timestamp).to.be.a('number');
    pm.expect(timestamp).to.be.greaterThan(0);
    console.log('Timestamp:', timestamp);
});
OutputSuccess
Important Notes

Timestamps are always in milliseconds since 1970-01-01 UTC.

Use environment or global variables to reuse timestamps across requests.

Printing timestamps helps debug test timing issues.

Summary

Timestamps record the exact time for test events.

Use Date.now() in Postman scripts to get the current time.

Save timestamps in variables to use them later in your tests.