This trick is not known by everyone though it is really simple.
As easy as that.
The only thing I add is the void(0); at the end of the code to avoid a “bug” that reloads the page.
Hey, if you want to write a bookmarklet, you'll probably use this technique.
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?
This one will be widely accepted. The problem with it is that it's so obvious that the question is asked here and there.
But anyone could override the function to always return true and all the dates that he enters would be good. This is bad.
If you have little time to implement an application, just do the server-side validations.
I don't really like it either…
I talked earlier about bookmarklets and how wonderful they are. In today's web2.0 world, you can't have a successful application without giving your users a cool bookmarklet. It can be easy to do sometimes (like the one of del.icio.us) but you always want to do more and it gets more complex… until the day you hit the wall…
Example, I want every background color of every web page I visit to be gray. I would create a script with this code
document.body.style.backgroundColor = "gray";
and it would just work.
I want all the solutions you can give to me wether it is real or fantasy.
The day I discovered it, I was really happy. Not happy as a fish in water but maybe happy as a cat having its meal (though it enormously depends of which cat we are talking about). Talking about bookmarklet to some of my friends, I saw that there were a lot of incomprehension about it. So I'll begin at the beginning.
var script = document.createElement("script");
Now, let your creativity flow and make a great application.