Timestamps help record the exact time an event happens. They are useful to track when tests run or when data is created.
Timestamp generation in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Postman
var timestamp = Date.now(); console.log(timestamp);
Postman
pm.environment.set('currentTimestamp', Date.now());
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); });
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.
Practice
1. What does
Date.now() return in Postman scripts?easy
Solution
Step 1: Understand Date.now() function
Date.now()returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.Step 2: Compare options with this definition
Only The current timestamp in milliseconds since January 1, 1970 correctly describes this behavior as a timestamp in milliseconds.Final Answer:
The current timestamp in milliseconds since January 1, 1970 -> Option CQuick Check:
Date.now() = milliseconds timestamp [OK]
Hint: Remember: Date.now() gives milliseconds since 1970 [OK]
Common Mistakes:
- Thinking Date.now() returns a formatted date string
- Confusing milliseconds with seconds
- Assuming it returns time zone info
2. Which of the following is the correct way to save the current timestamp in a Postman environment variable?
easy
Solution
Step 1: Identify the correct method to set environment variables
In Postman scripts,pm.environment.set(key, value)is used to save a variable.Step 2: Check each option's method name
Only pm.environment.set('currentTime', Date.now()); uses the correct methodsetwith the right syntax.Final Answer:
pm.environment.set('currentTime', Date.now()); -> Option AQuick Check:
Use pm.environment.set() to save variables [OK]
Hint: Use pm.environment.set() to save variables [OK]
Common Mistakes:
- Using pm.environment.get() to save variables
- Using non-existent methods like save() or store()
- Forgetting to pass both key and value
3. What will be the output of this Postman test script snippet?
let start = Date.now();
pm.environment.set('startTime', start);
let end = Date.now();
pm.environment.set('endTime', end);
let duration = pm.environment.get('endTime') - pm.environment.get('startTime');
console.log(duration);medium
Solution
Step 1: Understand the timestamp capture
startandendcapture timestamps at two different moments, soend≥start.Step 2: Calculate duration
Subtractingstartfromendgives the elapsed time in milliseconds, which is positive or zero.Final Answer:
A positive number representing milliseconds elapsed between start and end -> Option DQuick Check:
duration = end - start ≥ 0 [OK]
Hint: Subtract start from end timestamps for elapsed time [OK]
Common Mistakes:
- Assuming start and end are identical
- Confusing subtraction order causing negative result
- Thinking variables are not saved properly
4. You wrote this Postman script to save a timestamp:
Why does this cause an error or unexpected behavior?
pm.environment.set('timeStamp', Date.now)Why does this cause an error or unexpected behavior?
medium
Solution
Step 1: Check usage of Date.now
Date.nowis a function reference, not the timestamp itself.Step 2: Identify missing parentheses
To get the current timestamp, you must call the function with(), likeDate.now().Final Answer:
Date.now is a function and needs parentheses to execute -> Option AQuick Check:
Call Date.now() with () to get timestamp [OK]
Hint: Always add () to call Date.now() function [OK]
Common Mistakes:
- Forgetting parentheses after Date.now
- Thinking pm.environment.set can't save numbers
- Assuming variable names cause errors
5. You want to measure the response time of an API request in Postman using timestamps. Which script correctly captures the start time before the request and calculates the duration after the response?
hard
Solution
Step 1: Capture start time before request
The Pre-request Script runs before the API call, so savingstartTimethere is correct.Step 2: Calculate duration after response
The Tests script runs after the response, so subtractingstartTimefrom current time gives elapsed time.Final Answer:
captures the start time before the request and calculates the duration after the response -> Option BQuick Check:
Pre-request sets start; Tests calculate duration [OK]
Hint: Set start time pre-request; calculate duration in tests [OK]
Common Mistakes:
- Setting start time after request instead of before
- Subtracting timestamps in wrong order
- Calculating duration before request runs
