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 is what I did in my code, I have just tested and it worked fine, input type="date" does not support to set curdate automatically, so the way I used to overcome this limitation was using PHP code a simple code like this.
Hope it helps!
use moment.js to solve this issue in 2 lines, html5 date input type only accept "YYYY-MM-DD" this format. I solve my problem this way.
this is simplest way to solve this issue.
If you are using ruby you can use this to set the default value to today's date and time:
HTML
JS
demo
If you don't want to use jQuery you can do something like this
HTML
JS
demo
There is no default method within HTML itself to insert todays date into the input field. However, like any other input field it will accept a value.
You can use PHP to fetch todays date and input it into the value field of the form element.
The value field accepts the format YYYY-MM-DD as an input so simply by creating a variable
$date_string
in the same format that the input value accepts and fill it with the year, month and day fetched from todays date and voilá! You have yourself a preselected date!Hope this helps :)
Edit:
If you would like to have the input field nested within HTML rather than PHP you could do the following.
I realise this question was asked a while back (2 years ago) but it still took me a while to find a definite answer out on the internet, so this goes to serve anyone who is looking for the answer whenever it may be and hope it helps everyone greatly :)
Another Edit:
Almost forgot, something thats been a royal pain for me in the past is always forgetting to set the default timezone whenever making a script in PHP that makes use of the date() function.
The syntax is
date_default_timezone_set(...);
. Documentation can be found here at PHP.net and the list of supported timezones to insert into the function can be found here. This was always annoying since I am in Australia, everything is always pushed back 10 hours if I didn't set this properly as it defaults to UTC+0000 where I need UTC+1000 so just be cautious :)