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