0

Hi I can set time in variable in postman and increase it based on the currenttimestamp.

var moment = require("moment");
pm.environment.set('scopeInTime', moment().add(3, 'd').add(6, 'h').add(30, 'minutes').toISOString());

However I want to increase the time +4 hours after each time I sent the request.

I also know how to increase integers, but I could not figure out how it should work with timestamps.

postman.setEnvironmentVariable('call-key', Number(postman.getEnvironmentVariable("call-key"))+1);

I also tried without succes

var moment = require("moment");
var fromTime = pm.variables.get("fromTime");
pm.environment.set('fromTime', moment().toISOString().add(4, 'hours').valueOf(fromTime));
console.log("fromTime", fromTime);

What I am trying to let it work:

fromTime: 2020-01-01T00:00:00.746Z
toTime:   2020-01-01T04:00:00.746Z

And each time I sent this request, it should add 4 hour on both timestamps. For example:

fromTime: 2020-01-01T04:00:00.746Z
toTime:   2020-01-01T08:00:00.746Z
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
EEO27
  • 11
  • 3
  • Does this answer your question? [How to generate future date and time for postman request](https://stackoverflow.com/questions/64311878/how-to-generate-future-date-and-time-for-postman-request) – Christian Baumann Nov 03 '20 at 14:45
  • @ChristianBaumann This did not worked for me, I couldn't configure it to work it in the way I need it. The way I need are two timestamps for example. fromTime: 2020-01-01T00:00:00.746Z toTime: 2020-01-01T04:00:00.746Z And each time I sent this request, it should add 4 hour on both timestamps. For example: fromTime: 2020-01-01T04:00:00.746Z toTime: 2020-01-01T08:00:00.746Z – EEO27 Nov 04 '20 at 08:49

2 Answers2

0

This will do it:

var fromTime1 = "2020-01-01T00:00:00.746Z";

var timestring1Moment = moment(fromTime1);

toTime1 = moment(timestring1Moment).add(4, 'hours');

console.log(toTime1);
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
0

This did it for me. Thanks for all input and helps!

In pre-request:

var moment = require('moment');

var fromTime = postman.getEnvironmentVariable("fromTime");
toTime = moment(fromTime).add(4, 'hours').toISOString();   

postman.setEnvironmentVariable("toTime", toTime);

In Tests:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("fromTime", jsonData.to)
EEO27
  • 11
  • 3