Timestamp mismatch between Pre-request Script output and {{$isoTimestamp}} resolution
0 reputation · 08 May 2025, 04:08 UTC
0 reputation · 08 May 2025, 04:08 UTC
Postman resolves dynamic variables such as {{$isoTimestamp}} at the moment of request transmission, producing a UTC ISO 8601 value. Pre-request Scripts, by contrast, run earlier in a JavaScript sandbox where the native Date object defaults to the local time zone of the machine running the client.
The goal is to send a request payload containing a timestamp converted to a specific regional time zone, while keeping it consistent with any value computed in the Pre-request Script. Because the two mechanisms evaluate time at different points and against different clocks (UTC versus local system time), the values can diverge, and hard-coding a fixed UTC offset breaks during Daylight Saving Time transitions. Assume a current Postman desktop client version; behavior should be verified against the installed release.
pm.environment.set() the reliable way to avoid the resolution-timing gap with dynamic variables?34775 reputation · 08 May 2025, 08:05 UTC
Generate the regional timestamp once inside the Pre-request Script, store it with pm.environment.set() or pm.collectionVariables.set(), and reference that stored variable in the request body. Do not use {{$isoTimestamp}} in the same request when the value has to match your script's logic. It is a separate clock read that always produces UTC, so it will not agree with a regional value even when the timing gap between the two reads is zero.
Confirmed: pre-request scripts run before the request is built and sent, and dynamic variables such as {{$isoTimestamp}} are resolved later, during request preparation. Confirmed: {{$isoTimestamp}} is UTC in ISO 8601 form. Confirmed: the sandbox exposes the standard JavaScript Date object, so new Date() reads the system clock of whichever process executes the script.
The likely explanation for the mismatch you are seeing is two separate reads of the clock, one in the script and one at resolution time, combined with a formatting difference. The timing gap is usually milliseconds to a second. The timezone difference is systematic: a regional value differs from UTC by the offset, not by a few milliseconds.
One clarification worth keeping: new Date().toISOString() is not local time. It converts the current instant to UTC. If your script output looks local, the formatting call is doing that, not toISOString().
America/New_York, never a numeric offset.const zone = 'America/New_York';
// 'sv-SE' yields YYYY-MM-DD HH:mm:ss, easy to normalise
const parts = new Date().toLocaleString('sv-SE', {
timeZone: zone,
hour12: false
});
const regionalTimestamp = parts.replace(' ', 'T');
pm.collectionVariables.set('regionalTimestamp', regionalTimestamp);
console.log('regionalTimestamp =', regionalTimestamp);Then in the body:
{
"timestamp": "{{regionalTimestamp}}"
}Passing a timeZone option to toLocaleString delegates the offset to the runtime's IANA timezone database, which applies the correct offset for that specific date, including DST transitions. A hard-coded +05:00 or -04:00 cannot do this. Note that the resulting string carries no offset suffix, so a consumer cannot tell which zone it represents. If the API requires an explicit offset or a trailing Z, either send UTC plus a separate zone field, or compute the offset for that instant and append it.
{{$isoTimestamp}} and a script-set new Date().toISOString() in a scratch request and compare. Expect a small delta and a UTC value.Where does the request actually execute: the desktop client on your machine, or a cloud or agent runner? If it is an agent, the clock read by new Date() belongs to that host, not to you, and the stored value will reflect it. Confirm this before treating the stored timestamp as your regional time. The exact ordering of dynamic variable resolution should also be verified against your installed Postman release, since it is not a documented contract and may change between versions.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 08 May 2025, 10:34 UTC
To keep a single regional timestamp consistent, generate it in the Pre‑request Script using Intl.DateTimeFormat with the desired IANA zone (e.g., new Intl.DateTimeFormat('sv-SE', { timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }).format(new Date())). Store the result with pm.variables.set('regionalTs', formatted) – request‑scoped variables are cleared after the request, avoiding leakage. Then reference {{regionalTs}} in the body; do not mix with {{$isoTimestamp}} as it resolves later and always yields UTC.