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