Pre-request Script to set a timestamp 180 days in the past from today

I am trying to set a variable in a collection which will pass the value of a time and date stamp 180 days in the past from today. Eg. Today is 15th June 2023, I want the format to be: YYYY-MM-DDTHH:mm:SS. The returned value should be: 2022-12-17T13:58:12

Can you please also explain how I would need to setup the variables in the Collection

So far this is what I have managed to do but it doesnt work.

Pre-request Script:

var moment = require(‘moment’)

pm.variables.set(‘timestamp’, moment().format((“YYYY-MM-DDTHH:mm:SS”)));

This is the error when I try and run a GET request:
Screenshot 2023-06-15 at 2.03.08 pm

The only Variable I have is:
timestamp
There are no values against this.

Apologies, I am quite new to this and trying to read and learn but this coding is quite complex to me. Any help is much appreciated.

I don’t think there is anything particularly wrong with your code (apart from its not subtracting the 180 days).

var moment = require("moment")

pm.collectionVariables.set("timestamp", moment().subtract(180, 'days').format(("YYYY-MM-DDTHH:mm:SS")));
// or pm.variables.set("timestamp", moment().subtract(180, 'days').format(("YYYY-MM-DDTHH:mm:SS")));

console.log(pm.collectionVariables.get("timestamp"));

Do you have any other code in the pre-request tab as I don’t think this is triggering that error message.