how do I subtract one week from this date in jquery?
You can modify a date using setDate
. It automatically corrects for shifting to new months/years etc.
var oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
And then go ahead to render the date to a string in any matter you prefer.
I'd do something like
var myDate = new Date();
var newDate = new Date(myDate.getTime() - (60*60*24*7*1000));
var now = new Date();
now.setDate(now.getDate() - 7); // add -7 days to your date variable
alert(now);
Check out Date.js. Its really neat!
http://www.datejs.com/
Here are a couple of ways to do it using Date.js:
// today - 7 days
// toString() is just to print it to the console all pretty
Date.parse("t - 7 d").toString("MM-dd-yyyy"); // outputs "12-06-2011"
Date.today().addDays(-7).toString("MM-dd-yyyy"); // outputs "12-06-2011"
Date.today().addWeeks(-1).toString("MM-dd-yyyy"); // outputs "12-06-2011"
As an unrelated side note, do check out Moment.js as well... I think the 2 libraries compliment each other :)
http://momentjs.com/