When you create a Date object, it is initialized at now. To alert the primitive value (the number of milliseconds since 1970), I simply do.
var now = new Date(); alert(now.valueOf());
This one is easy. When I want to add a number of days (or anything else) to a date, I use the getDate() and setDate() function. When you don't know that the setDate() function (and setHours, setMinutes, setMonths…) has some magic in it, you may have a hard time manipulating the dates.
So, I want to add 3 days to the current date.
var now = new Date();// Add 3 days now.setDate(now.getDate() + 3); alert(now);The problem is when you want to add 3 days to December 31 2006, it should return January 3 2007. No problemo!
var now = new Date();You see that the date is correctly computed. You can do the same things with all the other parts of the date. Magic is in the air!
// Set to December 31 2006 now.setYear(2006); now.setMonth(11); now.setDate(31);
// Add 3 days now.setDate(now.getDate() + 3);
If you want to display the YYYY-MM-DD format, you'll have to do this ugly code.
var now = new Date();var nowStr = now.getFullYear().toString() + "-" + (now.getMonth()+1 < 10 ? "0" + (now.getMonth()+1).toString() : (now.getMonth()+1).toString()) + "-" + (now.getDate() < 10 ? "0" + now.getDate().toString() : now.getDate().toString());Maybe you won't do it as ugly as this but it won't look pretty. Maybe you should check for a librairy.
Do you have something else to add?