Timestamp generation in Postman - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
/* Pre-request Script */ const timestamp = Date.now(); pm.variables.set('currentTimestamp', timestamp); /* Tests Script */ const timestamp = parseInt(pm.variables.get('currentTimestamp'), 10); pm.test('Timestamp is a number', () => { pm.expect(timestamp).to.be.a('number'); }); pm.test('Timestamp is recent', () => { const now = Date.now(); pm.expect(timestamp).to.be.within(now - 5000, now + 5000); });
In the Pre-request Script, we generate the current timestamp using Date.now() which returns milliseconds since 1970. We store it in a Postman variable currentTimestamp so it can be accessed later.
In the Tests tab, we retrieve the stored timestamp and convert it to a number. We then check two things: first, that it is a number type, and second, that it is within 5 seconds of the current time to ensure it is recent. This confirms the timestamp generation is working correctly.
We use pm.expect for assertions as recommended in Postman scripts. The code is kept simple and clear for easy understanding and maintenance.
Now add data-driven testing with 3 different time offsets (e.g., current time, 1 minute ago, 1 minute ahead) and verify timestamps accordingly
Practice
Date.now() return in Postman scripts?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]
- Thinking Date.now() returns a formatted date string
- Confusing milliseconds with seconds
- Assuming it returns time zone info
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]
- Using pm.environment.get() to save variables
- Using non-existent methods like save() or store()
- Forgetting to pass both key and value
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);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]
- Assuming start and end are identical
- Confusing subtraction order causing negative result
- Thinking variables are not saved properly
pm.environment.set('timeStamp', Date.now)Why does this cause an error or unexpected behavior?
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]
- Forgetting parentheses after Date.now
- Thinking pm.environment.set can't save numbers
- Assuming variable names cause errors
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]
- Setting start time after request instead of before
- Subtracting timestamps in wrong order
- Calculating duration before request runs
