How do I get current ISO8601 timestamp in Postman Pre-Query Script and Body?
Asked Answered
C

2

13

Using Postman and the following code in pre-query script

   const moment = require('moment');
   pm.globals.set("timestamp", moment().format("YYYY-MM-DDTHH:MM:SSZ"));

I get as a response

Request signature is too far in the past and has expired. Timestamp date: 2019-11-30T10:11:10+00:00

In body I am using {{timestamp}}.

I need timestamp in ISO8601 format.

If I use

    {{$timestamp}}

it returns a Linux date of 1575110444 which is correct today at 10:41

Complacency answered 30/11, 2019 at 10:44 Comment(0)
R
16

Check this out postman inbuilt variables

{{$timestamp}} is postman's inbuilt dynamic variables which will return always unix timestamp. what you can do is rename the variable you setting -

const moment = require('moment');
pm.globals.set("timestamp1", moment().format("YYYY-MM-DDTHH:mm:ssZ"));

and use it {{timestamp1}}

Notice the $ sign in the inbuilt variable.

check this thread for more info

Update

From June 2020 onwards there is inbuilt variable provided for this as $isoTimestamp- for more details refer the documentation - https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#common

Rattish answered 30/11, 2019 at 12:56 Comment(3)
Thanks for your answer that resolved that issue. I had supposed that the $ might mean it was Linux syntax but I'm no expert and I didn't want to assume. Now onto a signature issue. I'm new to all this so am doing stacks of research to try and self-teach before bombarding others with my problems.Complacency
Also, I've now shortened it to const moment = require('moment'); pm.globals.set("timestamp", moment().format()); and it still seems to work fine!Complacency
Shouldn't the format be "YYYY-MM-DDTHH:mm:ssZ" instead?Villiers
E
31

It seems there is now an $isoTimestamp variable available out of the box

Encomiastic answered 2/11, 2020 at 12:21 Comment(1)
This gives current GMT date time. What if I need current local datetime in ISO format?Nefertiti
R
16

Check this out postman inbuilt variables

{{$timestamp}} is postman's inbuilt dynamic variables which will return always unix timestamp. what you can do is rename the variable you setting -

const moment = require('moment');
pm.globals.set("timestamp1", moment().format("YYYY-MM-DDTHH:mm:ssZ"));

and use it {{timestamp1}}

Notice the $ sign in the inbuilt variable.

check this thread for more info

Update

From June 2020 onwards there is inbuilt variable provided for this as $isoTimestamp- for more details refer the documentation - https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#common

Rattish answered 30/11, 2019 at 12:56 Comment(3)
Thanks for your answer that resolved that issue. I had supposed that the $ might mean it was Linux syntax but I'm no expert and I didn't want to assume. Now onto a signature issue. I'm new to all this so am doing stacks of research to try and self-teach before bombarding others with my problems.Complacency
Also, I've now shortened it to const moment = require('moment'); pm.globals.set("timestamp", moment().format()); and it still seems to work fine!Complacency
Shouldn't the format be "YYYY-MM-DDTHH:mm:ssZ" instead?Villiers

© 2022 - 2024 — McMap. All rights reserved.