The new HTML5 input types are great. Opera's new built-in date picker is a breeze, and Chrome has at least supported the new input type with a spin-wheel implementation.
But is there any way to set the default value of the date field to today's date? With Opera, I can choose 'Today' from the date picker, and as soon as I click on either of the step buttons in Chrome, it increments/decrements from today's date.
I'm not shy to code a solution to this minor problem, but it seems silly to me that both of the browsers are fully aware of the current date but won't automatically just pop it in (at least as a placeholder).
For NodeJS (Express with SWIG templates):
If you're doing anything related to date and time in the brower, you want to use Moment.js:
moment()
returns an object representing the current date and time. You then call its.format()
method to get a string representation according to the specified format. In this case,YYYY-MM-DD
.Full example:
I had the same problem and I fixed it with simple JS. The input:
the JS
Important: the JS script should be in the last code line, or after to input, because if you put this code before, the script won't find your input.
Since there's no default method of setting the value to today's date, I would say this should be dependent upon it's application. If you're looking to maximize your audience's exposure to the date picker, then use a server-side script (PHP, ASP, etc.) to set the default value.
However, if it's for the administration console of the CMS, and you know that the user will always have JS on or your site trusted, then you can safely use JS to fill the default value, as per jlbruno.
Like any HTML input field, the browser will leave it empty unless a default value is specified with the
value
attribute.Unfortunately HTML5 doesn't provide a way of specifying 'today' in the
value
attribute (that I can see), only a RFC3339 valid date like2011-09-29
. Sorry, that doesn't help much!if you need to fill input datetime you can use this: