# Need Previous month last date in variable

**URL:** <https://community.postman.com/t/need-previous-month-last-date-in-variable/25901>\
**Category:** Help Hub\
**Tags:** tests, pre-request, pre-request-script\
**Created:** [July 19, 2021, 7:01pm UTC](https://community.postman.com/t/need-previous-month-last-date-in-variable/25901 "2021-07-19T19:01:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhaskervade](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@bhaskervade](https://community.postman.com/u/bhaskervade)\
**Post date:** [July 19, 2021, 7:01pm UTC](https://community.postman.com/t/need-previous-month-last-date-in-variable/25901/1 "2021-07-19T19:01:30Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![pranavdavar](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pranavdavar/32/9152_2.png) [@pranavdavar](https://community.postman.com/u/pranavdavar)\
**Post date:** [July 20, 2021, 4:09am UTC](https://community.postman.com/t/need-previous-month-last-date-in-variable/25901/2 "2021-07-20T04:09:53Z")

</div>

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.

```auto
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](https://www.postman.com/deciphermiddleware/workspace/deciphermiddleware/request/1913239-cf60d2a2-cd5d-41e9-8645-a37678b1088c).

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![bhaskervade](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@bhaskervade](https://community.postman.com/u/bhaskervade)\
**Post date:** [July 21, 2021, 3:14pm UTC](https://community.postman.com/t/need-previous-month-last-date-in-variable/25901/3 "2021-07-21T15:14:18Z")

</div>

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);
```
