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

Leave a Comment