Postman TodayDate variable to get past dates

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

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

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:

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

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

2 Likes

@bpricilla thanks so much for the help ! That works great ! your the best !

2 Likes

People helping people! I love to see it :slight_smile:

@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

Great to hear :blush: Cheers!! Happie Testing :partying_face: