Need Previous month last date in variable

Hi Team,

can anyone help me - pre-request script

In my GET request when I ever hit the call date should be previous month last date, date parameter stored in a variable

the same request runs daily to make sure it should be last month-end date

For example: if I hit in August, I need July last date in request, etc…

Hi @bhaskervade, Welcome to the community!

This is really an interesting query. You can use moment js to accomplish this. Please find below a code snippet that can be used in the pre-request script for reference.

const moment = require('moment');
var offset= moment().date();
console.log(moment().subtract(offset,'days'));

You may set the log data to some environment/collection/global variable and use it in request.

I have also added this use case to my public workspace, you can try it out from there directly using the link.

Hope this helps :slight_smile:

3 Likes

Thanks Pranav ,

I used below snippet it worked

var curr_dt=new Date();

    curr_dt.setDate(1);

    curr_dt.setHours(-1); 

    day = curr_dt.getDate();

    month = curr_dt.getMonth() + 1;

    if (month <=9)

    {

        month = "0" + month;

    }

    year = curr_dt.getFullYear();

    last_day = year + "-" + month + "-" + day;

    pm.environment.set("prevmonthenddate", last_day);