How to set date format in HTML date input tag?
I am wondering if it is possible to set the date format in the html <input type="date"></input> ... Currently it is yyyy-mm-dd, while I need it in the dd-mm-yyyy format .
I think there is no solution if using only HTML I think you should use jquery
suitable jquery jQuery Masked Input plugin
No.
Firstly, your question is ambiguous - do you mean the format in which it is displayed to the user, or the format in which it is transmitted to the web server?
If you mean the format in which it is displayed to the user, it depends on the end-user interface, and not what you specify in HTML. Usually I expect it to be based on the date format that is set in the locale of the operating system. It makes no sense to try to redefine it in your preferred format, since the format that it displays is (generally speaking) the correct format for the user's locale and the format that the user uses to write / understand dates.
If you mean the format in which it is sent to the server, you are trying to fix the wrong problem. What you need to do is program the server-side code to accept dates in yyyy-mm-dd format.
I found the same question or related question in stackoverflow
Is there any way to change input type = date format?
I found one simple solution, you canโt give a particle format, but you can set it up.
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:
If you are using jQuery, here is a simple simple method
$("#dateField").val(new Date().toISOString().substring(0, 10)); Or there is the old traditional way:
document.getElementById("dateField").value = new Date().toISOString().substring(0, 10) Why not use the html5 control, as it is, with other attributes that allow it to work fine in browsers that support the date type, and still works in other browsers like firefox that don't yet support the date type.
<input type="date" name="input1" placeholder="YYYY-MM-DD" required pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" title="Enter a date in this formart YYYY-MM-DD"/> I think that in HTML there is no syntax for the DD-MM-YYYY format. See This Page http://www.java2s.com/Code/SQLServer/Date-Timezone/FormatdateMmmddyyyyhhmmdp.htm . In a way, this will help you. Else use javascript date picker.
I donโt know this for sure, but I think that this should be handled by the browser based on the userโs date / time settings. Try setting up your computer to display dates in this format.
$('input[type="date"]').change(function(){ alert(this.value.split("-").reverse().join("-")); }); I have done a lot of research, and I donโt think itโs possible to force the format <input type="date"> . The browser selects the local format, and depending on the user's settings, if the user's language is in English, the date will be displayed in English format (mm / dd / yyyy).
In my opinion, the best solution is to use a plugin to control the display.
Jquery DatePicker seems like a good option, as you can force localization , date format ...
I came up with a slightly different answer than everything I read above.
My solution uses a bit of JavaScript and HTML input.
The date received by the input leads to a string formatted as "yyyy-mm-dd." We can split it and place it correctly as a new date ().
Here is the basic HTML:
<form id="userForm"> <input type="text" placeholder="1, 2, 3, 4, 5, 6" id="userNumbers" /> <input type="date" id="userDate" /> <input type="submit" value="Track" /> </form> Here is the main JS:
let userDate = document.getElementById('userDate').value.split('-'), parsedDate = new Date(('${userDate[1]}-${userDate[2]}-${userDate[0]}')); You can format the date in any way convenient for you. You can create a new Date () and get all the information from there ... or just use 'userDate []' to build your string.
Keep in mind that some ways to enter a date in 'new Date ()' produce an unexpected result. (that is - one day behind)
Pure implementation without dependencies. https://jsfiddle.net/h3hqydxa/1/
<style type="text/css"> .dateField { width: 150px; height: 32px; border: none; position: absolute; top: 32px; left: 32px; opacity: 0.5; font-size: 12px; font-weight: 300; font-family: Arial; opacity: 0; } .fakeDateField { width: 100px; /* less, so we don't intercept click on browser chrome for date picker. could also change this at runtime */ height: 32px; /* same as above */ border: none; position: absolute; /* position overtop the date field */ top: 32px; left: 32px; display: visible; font-size: 12px; /* same as above */ font-weight: 300; /* same as above */ font-family: Arial; /* same as above */ line-height: 32px; /* for vertical centering */ } </style> <input class="dateField" id="dateField" type="date"></input> <div id="fakeDateField" class="fakeDateField"><em>(initial value)</em></div> <script> var dateField = document.getElementById("dateField"); var fakeDateField = document.getElementById("fakeDateField"); fakeDateField.addEventListener("click", function() { document.getElementById("dateField").focus(); }, false); dateField.addEventListener("focus", function() { fakeDateField.style.opacity = 0; dateField.style.opacity = 1; }); dateField.addEventListener("blur", function() { fakeDateField.style.opacity = 1; dateField.style.opacity = 0; }); dateField.addEventListener("change", function() { // this method could be replaced by momentJS stuff if (dateField.value.length < 1) { return; } var date = new Date(dateField.value); var day = date.getUTCDate(); var month = date.getUTCMonth() + 1; //zero-based month values fakeDateField.innerHTML = (month < 10 ? "0" : "") + month + " / " + day; }); </script> The short, direct answer is no or no, but I suggested a method for using a text field and pure JS code to simulate date input and execute whatever format you want. Here is the code
<html> <body> date : <span style="position: relative;display: inline-block;border: 1px solid #a9a9a9;height: 24px;width: 500px"> <input type="date" class="xDateContainer" onchange="setCorrect(this,'xTime');" style="position: absolute; opacity: 0.0;height: 100%;width: 100%;"><input type="text" id="xTime" name="xTime" value="dd / mm / yyyy" style="border: none;height: 90%;" tabindex="-1"><span style="display: inline-block;width: 20px;z-index: 2;float: right;padding-top: 3px;" tabindex="-1">▼</span> </span> <script language="javascript"> var matchEnterdDate=0; //function to set back date opacity for non supported browsers window.onload =function(){ var input = document.createElement('input'); input.setAttribute('type','date'); input.setAttribute('value', 'some text'); if(input.value === "some text"){ allDates = document.getElementsByClassName("xDateContainer"); matchEnterdDate=1; for (var i = 0; i < allDates.length; i++) { allDates[i].style.opacity = "1"; } } } //function to convert enterd date to any format function setCorrect(xObj,xTraget){ var date = new Date(xObj.value); var month = date.getMonth(); var day = date.getDate(); var year = date.getFullYear(); if(month!='NaN'){ document.getElementById(xTraget).value=day+" / "+month+" / "+year; }else{ if(matchEnterdDate==1){document.getElementById(xTraget).value=xObj.value;} } } </script> </body> </html> 1- note that this method only works for a browser that supports date type.
2- the first function in the JS code is for a browser that does not support the date type and sets the appearance for regular text input.
3- if you will use this code to enter several dates on your page, change the identifier "xTime" to enter text both when calling the function and when entering directly to something else, and, of course, use the input name for which Do you want a shipping form.
4-in the second function, you can use any format that you want instead of day + "/" + month + "/" + year for example year + "/" + month + "/" + day and in the text input use a placeholder or value in the form yyyy / mm / dd for the user when loading the page.
For formatting in mm-dd-yyyy
aa = date.split ("-")
date = aa [1] + '-' + aa [2] + '-' + aa [0]
<html> <body> <p id="result">result</p>Enter some text: <input type="date" name="txt" id="value" onchange="myFunction(value)"> <button onclick="f()">submit</button> <script> var a;function myFunction(val){a = val.split("-").reverse().join("-");document.getElementById("value").type = "text";document.getElementById("value").value = a;}function f(){document.getElementById("result").innerHTML = a;var z = a.split("-").reverse().join("-");document.getElementById("value").type = "date";document.getElementById("value").value = z;} </script> </body> </html> Here is the solution:
<input type="text" id="end_dt"/> $(document).ready(function () { $("#end_dt").datepicker({ dateFormat: "MM/dd/yyyy" }); }); hope this solves the problem :)
Actually there is, but no one seems to have bothered to support him. You were right <input type="date"> . In case someone doubts me. http://www.w3schools.com/html/html5_form_input_types.asp
The date value format is 'YYYY-MM-DD' . See the following example.
<form> <input value="2015-11-30" name='birthdate' type='date' class="form-control" placeholder="Date de naissance"/> </form> All you need to do is format the date in php, asp, ruby โโor whatever else to have this format.
