# Converting $timestamp to ISO 8601

**URL:** <https://community.postman.com/t/converting-timestamp-to-iso-8601/5488>\
**Category:** Help Hub\
**Created:** [May 17, 2019, 5:35pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488 "2019-05-17T17:35:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![floripare](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/floripare/32/1836_2.png) [@floripare](https://community.postman.com/u/floripare)\
**Post date:** [May 17, 2019, 5:35pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/1 "2019-05-17T17:35:20Z")

</div>

How can I convert $timestamp to ISO8601 format in a pre-request script?

This [page on Stack Overflow](https://stackoverflow.com/a/12868272/8183887) has a function to do it, but neither Date() nor toISOString() seem to be available.

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/matt/32/2562_2.png) [@matt](https://community.postman.com/u/matt)\
**Post date:** [May 17, 2019, 6:36pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/2 "2019-05-17T18:36:36Z")

</div>

Dynamic variables (like $timestamp) cannot be used in the Sandbox. You can only use them in the `{{..}}` format in the request URL / headers / body.  
[https://learning.getpostman.com/docs/postman/environments\_and\_globals/variables/#dynamic-variables](https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#dynamic-variables).

Try:

```auto
var dateIso = new Date().toISOString();

```

Good reference: [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Date/toISOString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)

---

<div class="post-metadata">

**Author:** ![shamasis](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/shamasis/32/53_2.png) [@shamasis](https://community.postman.com/u/shamasis)\
**Post date:** [May 17, 2019, 7:33pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/3 "2019-05-17T19:33:24Z")

</div>

Me too!! I mean… for all ISO date needs, I fall back to the code snippet that @matt posted.

However, if I have to do heavy date lifting, Postman Sandbox has the powerful “moment” library built in - more at [https://momentjs.com/](https://momentjs.com/)

```auto
var moment = require('moment');
moment().format();

```

Docs of moment: [http://momentjs.com/docs/#/displaying/as-iso-string/](http://momentjs.com/docs/#/displaying/as-iso-string/)

---

<div class="post-metadata">

**Author:** ![floripare](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/floripare/32/1836_2.png) [@floripare](https://community.postman.com/u/floripare)\
**Post date:** [May 17, 2019, 7:45pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/4 "2019-05-17T19:45:33Z")

</div>

Thank you. That worked beautifully!

@shamasis, Thank you, I’ll check that when I have some free time.

---

<div class="post-metadata">

**Author:** ![murdoc-she-wrote](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/murdoc-she-wrote/32/2289_2.png) [@murdoc-she-wrote](https://community.postman.com/u/murdoc-she-wrote)\
**Post date:** [May 28, 2019, 8:18pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/5 "2019-05-28T20:18:29Z")

</div>

Moment is pretty great.

I use this pre-request snippet to generate a timestamp from the previous day:

```
var moment = require('moment');
var ISO_8601_OFFSET = 'YYYY-MM-DDTHH:mm:ss.SSS';
var momentdate = moment().subtract(1, 'days').format(ISO_8601_OFFSET);

pm.environment.set("timestampUtcIso8601", momentdate+'Z');
```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [May 29, 2019, 8:17am UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/6 "2019-05-29T08:17:40Z")

</div>

Awesome stuff @murdoc-she-wrote,

You _could_ reduce that down even further 😉

```
var moment = require('moment');
pm.environment.set("timestampUtcIso8601more", `${moment().subtract(1, 'days').format()}Z`);

```

---

<div class="post-metadata">

**Author:** ![krysprendergast](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/krysprendergast/32/13003_2.png) [@krysprendergast](https://community.postman.com/u/krysprendergast)\
**Post date:** [August 24, 2022, 12:19pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/7 "2022-08-24T12:19:52Z")

</div>

Just updating in case someone goes to use moment. This is now as Moment.js describe in their documentation a “done” project, they will not be updating it in the future. More info: [Moment.js | Docs](https://momentjs.com/docs/#/-project-status/)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 23, 2023, 2:52pm UTC](https://community.postman.com/t/converting-timestamp-to-iso-8601/5488/8 "2023-08-23T14:52:41Z")

</div>

Closed on August 23, 2023, 2:52pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
