How to get a Timestamp formated with Regex

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 :wink:

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 - ???

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

For example.

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);

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.

1 Like

No probs, glad its now working for you.

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