Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value to the input[type="date"] [duplicate]

People also ask

How do you make input type date required?

The Input Date required property is used for setting or returning whether a date field must be filled out before submitting a form. The HTML required attribute is used to reflect the Input Date required property.

How do I change date input date?

Use the value property on the input elements of type date , time and datetime-local to set their values, e.g. dateInput. value = '2026-06-24' . The value property can be used to get and set the value of an input type date , time and datetime-local . Here is the HTML for the examples in this article.


The date should take the format YYYY-MM-DD. Single digit days and months should be padded with a 0. January is 01.

From the documentation:

A string representing a date.

Value: A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits representing a number greater than 0.

Your code should be altered to:

<input type="date" value="2013-01-08">

Example jsfiddle


<input type="date" id="myDate" />

Then in js :

_today: function () {
  var myDate = document.querySelector(myDate);
  var today = new Date();
  myDate.value = today.toISOString().substr(0, 10);
},

A possible solution:

document.getElementById("yourDatePicker").valueAsDate = new Date();

Using Moment.js:

var today = moment().format('YYYY-MM-DD');
document.getElementById("datePicker").value = today;

if you are using PHP, you can set the value like this

<input type="date" value="<?php echo date("Y-m-d");?>">

but remember that it would return the date of the server. e.g. if your server in USA and your client in Indonesia, it may differ 1 day.

But if you want to use from the client, use javascript solution instead. hope it helps.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!