I have a start calendar input box and an end calendar input box. We want defaults start calendar input box 30 days prior to current date and the end calendar input box to be the current date. Here is my date vars.
var today = new Date(), dd = today.getDate(), mm = today.getMonth(), yyyy = today.getFullYear(), month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October" "November", "December"], startdate = month[mm] + ", " + yyyy.toString();
The end date would be something like var enddate = startdate - 30;
Obviously this won’t work.
So if the current date is December 30, 2011 I’d want the start date to read December 1, 2011.
EDIT: My question was answered… sort of. Date.today();
and Date.today().add(-30);
work but I need the date in the format of January 13, 2012
. Not Fri Jan 13 2012 10:48:56 GMT -055 (EST)
. Any help?
MORE EDIT: As of this writing it’s 2018. Just use Moment.js. It’s the best.
Advertisement
Answer
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');