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).
this works for me:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
And for those using ASP VBScript
And then in the body, your element should look something like this
Cheers!
by Javascript:
In HTML5 as such, there is no way to set the default value of the date field to today’s date? As shown in other answers, the value can be set using JavaScript, and this is usually the best approach if you wish to set the default according to what is current date to the user when the page is loaded.
HTML5 defines the
valueAsDate
property forinput type=date
elements, and using it, you could set the initial value directly from an object created e.g. bynew Date()
. However, e.g. IE 10 does not know that property. (It also lacks genuine support toinput type=date
, but that’s a different issue.)So in practice you need to set the
value
property, and it must be in ISO 8601 conformant notation. Nowadays this can be done rather easily, since we can expect currenty used browsers to support thetoISOString
method:Thanks peter, now i change my code.