Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set date format in HTML date input tag?

I am wondering whether it is possible to set the date format in the html <input type="date"></input> tag... Currently it is yyyy-mm-dd, while I need it in the dd-mm-yyyy format.

like image 563
Piotr Avatar asked Aug 08 '11 06:08

Piotr


People also ask

How do you input a date in HTML?

The <input type="date"> defines a date picker. The resulting value includes the year, month, and day. Tip: Always add the <label> tag for best accessibility practices!

How do I restrict a date format in HTML?

You can add a min or max attribute to the input type=date . The date must be in ISO format (yyyy-mm-dd). This is supported in many mobile browsers and current versions of Chrome, although users can manually enter an invalid date without using the datepicker.


2 Answers

I found same question or related question on stackoverflow

Is there any way to change input type=“date” format?

I found one simple solution, You can not give particulate Format but you can customize Like this.

HTML Code:

    <body> <input type="date" id="dt" onchange="mydate1();" hidden/> <input type="text" id="ndt"  onclick="mydate();" hidden /> <input type="button" Value="Date" onclick="mydate();" /> </body> 

CSS Code:

#dt{text-indent: -500px;height:25px; width:200px;} 

Javascript Code :

function mydate() {   //alert(""); document.getElementById("dt").hidden=false; document.getElementById("ndt").hidden=true; } function mydate1() {  d=new Date(document.getElementById("dt").value); dt=d.getDate(); mn=d.getMonth(); mn++; yy=d.getFullYear(); document.getElementById("ndt").value=dt+"/"+mn+"/"+yy document.getElementById("ndt").hidden=false; document.getElementById("dt").hidden=true; } 

Output:

enter image description here

like image 135
ashish bhatt Avatar answered Sep 24 '22 13:09

ashish bhatt


If you're using jQuery, here's a nice simple method

$("#dateField").val(new Date().toISOString().substring(0, 10)); 

Or there's the old traditional way:

document.getElementById("dateField").value = new Date().toISOString().substring(0, 10) 
like image 30
sean.boyer Avatar answered Sep 23 '22 13:09

sean.boyer