添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
安静的手套  ·  Postman flow - ...·  2 周前    · 
憨厚的西红柿  ·  Postman TodayDate ...·  2 周前    · 
大鼻子的海龟  ·  jq ...·  5 月前    · 
耍酷的企鹅  ·  CRISPR gRNA设计工具·  5 月前    · 
奋斗的烤土司  ·  jQuery 事件(三) ...·  8 月前    · 

Hi, so I’m stuck on creating a new variable for a past date. I currently have a global variable:
pm.globals.set(“todayDate”,parseInt(moment().format(“YYYYMMDD”)))

Instead of creating a bunch of different global variables for 5 days back, 10 days back and so on I was wondering what I can do with my todayDate variable to set a new local variable as 10 days back?

I know i could just do something like this: pm.globals.set(“yesterdayDate”,parseInt(moment().subtract(1, ‘days’).format(“YYYYMMDD”)))
but was thinking i could use my todayDate variable and manipulate that ?

Thanks!

@mattschmitt Welcome to the Community :partying_face:

You can use dynamic past date if there’s no specific req to the past date here. This will create new past data for every run

pm.environment.set("randompastdate", pm.variables.replaceIn('{{$randomDatePast}}'));
 let pastdate = pm.environment.get("randompastdate");
pastdate = moment(pastdate);
pm.environment.set("pastdate", pastdate.format("YYYYMMDD"));
 mattschmitt:

I know i could just do something like this: pm.globals.set(“yesterdayDate”,parseInt(moment().subtract(1, ‘days’).format(“YYYYMMDD”)))
but was thinking i could use my todayDate variable and manipulate that ?

Yes we can do like this.

var pdate = new Date();
pdate.setDate(pdate.getDate()-1);
pastdate = moment(pdate);
pm.environment.set("pastdate", pastdate.format("YYYYMMDD"));

So it depends on your req, Please let me know if you are still facing any issues :blush:

@bpricilla Thanks for the response. Sorry I’m pretty new to this but I have to get 10 days back from todaysDate so it does have to be a specific date.

What i have below does not work but could i do something along the lines of it do you know ?

// pm.variables.set(“tenDaysBack”, pm.globals.get(“todayDate”).subtract(10, ‘days’).format(“YYYYMMDD”))

@mattschmitt No problem :blush: I thought any past date would work for you.

So please try the below snippet then:

const moment = require('moment');
const today = moment();
var todayDate = today.format('YYYYMMDD');
pm.globals.set("todayDate", todayDate);
var tenDaysBack = moment().subtract(10, 'days');
//console.log(tenDaysBack);
pm.globals.set("tenDaysBack", tenDaysBack.format('YYYYMMDD'));

Please let me know if you still face any issues here :slightly_smiling_face:

I placed the snippet in Pre-request Script.
And added extra line to it pm.collectionVariables.set(“startDate”,“todayDate”);

In the body, i am using this line
“startDate”:“{{startDate}}”,

But its not picking it correctly, as, in console, I see as “startDate”:todayDate

Hey @sherrinjac :wave:

And added extra line to it pm.collectionVariables.set(“startDate”,“todayDate”);

Most likely you wanted the following instead:

pm.collectionVariables.set("startDate", todayDate);

See the lack of quotes (") around todayDate

If you are facing some other issue, I would suggest opening a new topic :+1: