Bird
Raised Fist0
Postmantesting~20 mins

Timestamp generation in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Timestamp Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Postman test script timestamp?
Consider this Postman test script snippet that generates a timestamp in milliseconds since Unix epoch. What will be the value of timestamp after running this code?
Postman
const timestamp = Date.now();
console.log(timestamp);
AUndefined, because Date.now() is not a function in Postman scripts
BA string with the current date in ISO format, e.g., '2023-06-17T12:00:00.000Z'
CAn object containing date and time properties
DA number representing milliseconds since January 1, 1970, e.g., 1687000000000
Attempts:
2 left
💡 Hint
Date.now() returns the current time in milliseconds as a number.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a timestamp is recent in Postman?
You want to write a test in Postman to check that a timestamp variable responseTimestamp is within the last 5 seconds. Which assertion is correct?
Postman
const responseTimestamp = pm.response.json().timestamp;
const now = Date.now();
Apm.expect(responseTimestamp).to.be.within(now - 5000, now);
Bpm.expect(responseTimestamp).to.equal(now - 5000);
Cpm.expect(responseTimestamp).to.be.above(now + 5000);
Dpm.expect(responseTimestamp).to.be.a('string');
Attempts:
2 left
💡 Hint
Check if the timestamp is between now minus 5 seconds and now.
🔧 Debug
advanced
2:00remaining
Why does this Postman script fail to generate a valid timestamp?
This Postman test script tries to generate a timestamp but fails. What is the error?
Postman
const timestamp = new Date().getTime;
console.log(timestamp);
Atimestamp is a function reference, not a number, because getTime is missing parentheses
Btimestamp is a string instead of a number
Ctimestamp is undefined because Date() is not called correctly
DSyntaxError due to missing semicolon
Attempts:
2 left
💡 Hint
Check if getTime is called as a function.
🧠 Conceptual
advanced
2:00remaining
What is the difference between Date.now() and new Date().getTime() in Postman scripts?
Both Date.now() and new Date().getTime() return timestamps. What is the key difference?
A<code>Date.now()</code> returns seconds; <code>new Date().getTime()</code> returns milliseconds
B<code>Date.now()</code> returns a string; <code>new Date().getTime()</code> returns a number
C<code>Date.now()</code> is a static method returning milliseconds; <code>new Date().getTime()</code> creates a Date object then returns milliseconds
DThere is no difference; both return the current date as a formatted string
Attempts:
2 left
💡 Hint
One creates an object, the other does not.
framework
expert
3:00remaining
How to reliably test timestamp freshness in Postman for asynchronous APIs?
You have an API that returns a timestamp of when data was last updated. The timestamp is in milliseconds. You want to write a Postman test that passes only if the timestamp is within 10 seconds of the current time, accounting for network delay. Which approach is best?
Postman
const responseTimestamp = pm.response.json().lastUpdated;
const now = Date.now();
AUse <code>pm.expect(responseTimestamp).to.be.a('string');</code> to verify type
BUse <code>pm.expect(responseTimestamp).to.be.within(now - 10000, now + 1000);</code> to allow 1 second future tolerance
CUse <code>pm.expect(responseTimestamp).to.equal(now);</code> for exact match
DUse <code>pm.expect(responseTimestamp).to.be.below(now - 10000);</code> to check if timestamp is older than 10 seconds
Attempts:
2 left
💡 Hint
Allow some tolerance for network delays and clock differences.

Practice

(1/5)
1. What does Date.now() return in Postman scripts?
easy
A. The current date as a string in format YYYY-MM-DD
B. The current time zone offset in minutes
C. The current timestamp in milliseconds since January 1, 1970
D. The current time in seconds since midnight

Solution

  1. Step 1: Understand Date.now() function

    Date.now() returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
  2. 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.
  3. Final Answer:

    The current timestamp in milliseconds since January 1, 1970 -> Option C
  4. Quick 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
A. pm.environment.set('currentTime', Date.now());
B. pm.environment.get('currentTime', Date.now());
C. pm.environment.save('currentTime', Date.now());
D. pm.environment.store('currentTime', Date.now());

Solution

  1. Step 1: Identify the correct method to set environment variables

    In Postman scripts, pm.environment.set(key, value) is used to save a variable.
  2. Step 2: Check each option's method name

    Only pm.environment.set('currentTime', Date.now()); uses the correct method set with the right syntax.
  3. Final Answer:

    pm.environment.set('currentTime', Date.now()); -> Option A
  4. Quick 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
A. A negative number due to subtraction order
B. Zero, because start and end are set at the same time
C. Undefined, because variables are not saved correctly
D. A positive number representing milliseconds elapsed between start and end

Solution

  1. Step 1: Understand the timestamp capture

    start and end capture timestamps at two different moments, so endstart.
  2. Step 2: Calculate duration

    Subtracting start from end gives the elapsed time in milliseconds, which is positive or zero.
  3. Final Answer:

    A positive number representing milliseconds elapsed between start and end -> Option D
  4. Quick 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:
pm.environment.set('timeStamp', Date.now)

Why does this cause an error or unexpected behavior?
medium
A. Date.now is a function and needs parentheses to execute
B. pm.environment.set() cannot save numbers
C. The variable name 'timeStamp' is reserved
D. Date.now returns a string, not a number

Solution

  1. Step 1: Check usage of Date.now

    Date.now is a function reference, not the timestamp itself.
  2. Step 2: Identify missing parentheses

    To get the current timestamp, you must call the function with (), like Date.now().
  3. Final Answer:

    Date.now is a function and needs parentheses to execute -> Option A
  4. Quick 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
A. In Tests: pm.environment.set('startTime', Date.now());
In Pre-request Script: let duration = Date.now() - pm.environment.get('startTime');
B. In Pre-request Script: pm.environment.set('startTime', Date.now());
In Tests: let duration = Date.now() - pm.environment.get('startTime'); pm.test('Response time', () => pm.expect(duration).to.be.below(2000));
C. In Pre-request Script: let duration = Date.now() - pm.environment.get('startTime');
In Tests: pm.environment.set('startTime', Date.now());
D. In Tests: let duration = pm.environment.get('startTime') - Date.now();
In Pre-request Script: pm.environment.set('startTime', Date.now());

Solution

  1. Step 1: Capture start time before request

    The Pre-request Script runs before the API call, so saving startTime there is correct.
  2. Step 2: Calculate duration after response

    The Tests script runs after the response, so subtracting startTime from current time gives elapsed time.
  3. Final Answer:

    captures the start time before the request and calculates the duration after the response -> Option B
  4. Quick 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