5

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?

Ritesh
  • 650
  • 1
  • 12
  • 23

2 Answers2

7

The value of input type="date" is always in YYYY-MM-DD format:

<input type="date" value="<?php print(date("Y-m-d")); ?>"/>
  • 2
    which was taken from here? http://stackoverflow.com/questions/6982692/html5-input-type-date-default-value-to-today. Have to admit that this is a standard line of code. – angabriel Dec 24 '13 at 13:00
2

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));
});​
Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70