# How to get a Timestamp formated with Regex

**URL:** <https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [July 28, 2023, 5:45pm UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050 "2023-07-28T17:45:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![1onord](https://avatars.discourse-cdn.com/v4/letter/1/0ea827/32.png) [@1onord](https://community.postman.com/u/1onord)\
**Post date:** [July 28, 2023, 5:45pm UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/1 "2023-07-28T17:45:52Z")

</div>

_My question_:

Hi Postman Pro`s,

i have an Endpoint which gives me an Timestamp, it shows like that:

2023-07-28T11:51:39+00:00

The Problem is that the API on the other Site wants this Timestamp in 2 Values:

one Value for the Time in Format: HHMMSS / 115139  
an the other Value for the Date in Format: YYYYMMDD / 20230728

_I’ve already tried_:

I tried to use a pre Request Script in the Second Request like that:

const moment = require(‘moment’);

pm.collectionVariables.set(“timestamp”, moment().format(“YYYYMMDD”));

pm.collectionVariables.set(“timestamp2”, moment().format(“HHMMSS”));

This gives me the actual Time and Date but dont modify the Source Timestamp which is also a Collection Variable in the First Request.

I dont understand how to do what moment does but with my Timestamp as Source not the actual Time an Date.

Maybe someone can explain the right Way 😉

---

<div class="post-metadata">

**Author:** ![1onord](https://avatars.discourse-cdn.com/v4/letter/1/0ea827/32.png) [@1onord](https://community.postman.com/u/1onord)\
**Post date:** [July 29, 2023, 10:47am UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/2 "2023-07-29T10:47:26Z")

</div>

I am a little bit further:

> let res = pm.response.json();  
> let date = res.gpsTime.split(‘T’)[0];  
> console.log(date)  
> 2023-07-28  
> pm.collectionVariables.set(“Date”, “date”);  
> let time = res.gpsTime.split(‘T’)[1].slice(0,8);  
> console.log(time)  
> 11:51:39  
> pm.collectionVariables.set(“Time”, “time”);

But how i can get out the : and - ???

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 31, 2023, 9:44am UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/3 "2023-07-31T09:44:38Z")

</div>

You need to pass in the response timestamp to the moment function.

For example.

```auto
let responseTimestamp = "2023-07-28T11:51:39+00:00";
// you need to retrieve this element from your response

const moment = require("moment");

let timestamp1 = moment(responseTimestamp).format("YYYYMMDD");
console.log(timestamp1); // "20230728"
pm.collectionVariables.set("timestamp1", timestamp1);

let timestamp2 = moment(responseTimestamp).format("hhmmss"); // needs to be lowercase
console.log(timestamp2); // "115139"
pm.collectionVariables.set("timestamp2", timestamp2);

```

---

<div class="post-metadata">

**Author:** ![1onord](https://avatars.discourse-cdn.com/v4/letter/1/0ea827/32.png) [@1onord](https://community.postman.com/u/1onord)\
**Post date:** [July 31, 2023, 10:17am UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/4 "2023-07-31T10:17:30Z")

</div>

Hi,

Thank you for your answer, it works perfectly. Oh man sometimes you’re blind on both eyes, I’m still learning.

I’ve read so many threads and pulled my hair out, the answer was probably just too simple.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 31, 2023, 12:37pm UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/5 "2023-07-31T12:37:02Z")

</div>

No probs, glad its now working for you.

---

<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:** [July 17, 2024, 6:24pm UTC](https://community.postman.com/t/how-to-get-a-timestamp-formated-with-regex/50050/6 "2024-07-17T18:24:37Z")

</div>

Closed on July 17, 2024, 6:24pm UTC

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