The new HTML5 input types are great. But is there any way to set the default value of the date field to today's date? I am working with html and php i want to set today date as a default date in date field of html5.
how can i do this?
The new HTML5 input types are great. But is there any way to set the default value of the date field to today's date? I am working with html and php i want to set today date as a default date in date field of html5.
how can i do this?
The value of input type="date"
is always in YYYY-MM-DD format:
<input type="date" value="<?php print(date("Y-m-d")); ?>"/>
Like any HTML input field, the browser will leave it empty unless a default value is specified with the value attribute. HTML5 doesn't provide a way of specifying 'today' in the value.
You can do it with javascript or php.
Here is a sample with PHP :
<input type="date" id="myDate" value="<?php echo date('Y-m-d');?>"/>
Here is a sample with Javascript :
document.getElementById('myDate').value = new Date().toJSON().slice(0,10);
Here is a sample with jquery :
$(document).ready( function() {
$('#myDate').val(new Date().toJSON().slice(0,10));
});