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!
1 Like
bpricilla
(Pricilla B)
April 5, 2021, 11:57pm
2
@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
1 Like
@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”))
2 Likes
bpricilla
(Pricilla B)
April 7, 2021, 3:54am
4
@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
3 Likes
@bpricilla thanks so much for the help ! That works great ! your the best !
2 Likes
People helping people! I love to see it
@mattschmitt if the above suggestion from @bpricilla worked for you, would you mind marking it as the solution so others can find it quickly?
3 Likes
bpricilla
(Pricilla B)
April 8, 2021, 3:39am
7
Great to hear Cheers!! Happie Testing
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
amit
(Amit Singh)
May 24, 2023, 4:21am
9
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
amit:
pm.collectionVariables.set("startDate", todayDate);
Thank you very much. It worked.
1 Like