How to get 30 days prior to current date?

To subtract days from a JS Date object you can use the setDate() method, along with the date to start the calculation from. This will return an epoch timestamp as an integer, so to convert this to a Date you'll need to again provide it to the Date() object constructor. The final example would look like this:

var today = new Date();
var priorDate = new Date(new Date().setDate(today.getDate() - 30));

console.log(today)
console.log(priorDate);

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

[Edit]

See also the excellent Moment.js JavaScript date library:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

Here's an ugly solution for you:

var date = new Date(new Date().setDate(new Date().getDate() - 30));

startDate = new Date(today.getTime() - 30*24*60*60*1000);

The .getTime() method returns a standard JS timestamp (milliseconds since Jan 1/1970) on which you can use regular math operations, which can be fed back to the Date object directly.


Get next 30th day from today

let now = new Date()
console.log('Today: ' + now.toUTCString())
let next30days = new Date(now.setDate(now.getDate() + 30))
console.log('Next 30th day: ' + next30days.toUTCString())

Get last 30th day form today

let now = new Date()
console.log('Today: ' + now.toUTCString())
let last30days = new Date(now.setDate(now.getDate() - 30))
console.log('Last 30th day: ' + last30days.toUTCString())