# Postman TodayDate variable to get past dates

**URL:** <https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290>\
**Category:** Help Hub\
**Tags:** variables, scripts\
**Created:** [April 5, 2021, 10:24pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290 "2021-04-05T22:24:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattschmitt](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mattschmitt](https://community.postman.com/u/mattschmitt)\
**Post date:** [April 5, 2021, 10:24pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/1 "2021-04-05T22:24:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [April 5, 2021, 11:57pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/2 "2021-04-05T23:57:50Z")

</div>

@mattschmitt Welcome to the Community 🥳

You can use [dynamic past date](https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#dates) if there’s no specific req to the past date here. This will create new past data for every run

```auto
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.

```auto
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 😊

---

<div class="post-metadata">

**Author:** ![mattschmitt](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mattschmitt](https://community.postman.com/u/mattschmitt)\
**Post date:** [April 6, 2021, 3:28pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/3 "2021-04-06T15:28:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [April 7, 2021, 3:54am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/4 "2021-04-07T03:54:08Z")

</div>

@mattschmitt No problem 😊 I thought any past date would work for you.

So please try the below snippet then:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![mattschmitt](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mattschmitt](https://community.postman.com/u/mattschmitt)\
**Post date:** [April 7, 2021, 3:18pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/5 "2021-04-07T15:18:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [April 7, 2021, 5:16pm UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/6 "2021-04-07T17:16:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [April 8, 2021, 3:39am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/7 "2021-04-08T03:39:30Z")

</div>

Great to hear 😊 Cheers!! Happie Testing 🥳

---

<div class="post-metadata">

**Author:** ![sherrinjac](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sherrinjac](https://community.postman.com/u/sherrinjac)\
**Post date:** [May 24, 2023, 12:39am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/8 "2023-05-24T00:39:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amit](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/amit/32/2056_2.png) [@amit](https://community.postman.com/u/amit)\
**Post date:** [May 24, 2023, 4:21am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/9 "2023-05-24T04:21:51Z")

</div>

Hey @sherrinjac 👋

> And added extra line to it pm.collectionVariables.set(“startDate”,“todayDate”);

Most likely you wanted the following instead:

```auto
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 👍

---

<div class="post-metadata">

**Author:** ![sherrinjac](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sherrinjac](https://community.postman.com/u/sherrinjac)\
**Post date:** [May 24, 2023, 4:38am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/10 "2023-05-24T04:38:04Z")

</div>

> [@amit](#):
>
> `pm.collectionVariables.set("startDate", todayDate);`

Thank you very much. It worked.

---

<div class="post-metadata">

**Author:** ![chantal-decraie](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@chantal-decraie](https://community.postman.com/u/chantal-decraie)\
**Post date:** [October 23, 2025, 9:40am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/11 "2025-10-23T09:40:33Z")

</div>

Hi @bpricilla the method “substract” is not working for me . I’m getting the error: your variable.substract is not a function….

Please advice!

```auto
// 1. Create a moment object for now

let now = require('moment');

const time = now().format(("YYYY-MM-DD") + "T" + ("HH:mm:ss")).split("+02:00")[0] + ".000Z";

//2. put this in the environment variable to be able to reuse this in as much places as possible

pm.environment.set('currentDate', time);

let momentPodPrevious = now();

let prevResultpod = momentPodPrevious.substract('days', 1);

pm.environment.set('momentPodPrev', prevResultpod);

```

Thank you!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 23, 2025, 10:18am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/12 "2025-10-23T10:18:47Z")

</div>

Hey @chantal-decraie 👋

Welcome to the Postman Community! :postman:

It looks like you have a slight typo on that function name `substract`, should be `subtract`.

I used the new Agent Mode to help me out and quickly find the issue in the script.

 ![Screenshot 2025-10-23 at 11.15.46](https://us1.discourse-cdn.com/getpostman/original/3X/f/7/f773da1cf811e16fe6c90177ecff81a27f25c536.png)

Once I confirmed the change, it saved and sent the request for me ❤

 ![Screenshot 2025-10-23 at 11.16.17](https://us1.discourse-cdn.com/getpostman/original/3X/6/e/6ebe5d2e9a227fac399aadf5703be8bdf9081803.png)

---

<div class="post-metadata">

**Author:** ![chantal-decraie](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@chantal-decraie](https://community.postman.com/u/chantal-decraie)\
**Post date:** [October 23, 2025, 11:03am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/13 "2025-10-23T11:03:13Z")

</div>

oh my days! @danny-dainton thank you! you’re so right 😉 😁 it’s fixed.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [October 23, 2025, 11:26am UTC](https://community.postman.com/t/postman-todaydate-variable-to-get-past-dates/22290/14 "2025-10-23T11:26:03Z")

</div>

Closed on October 23, 2025, 11:26am UTC


