How to set date format in HTML date input tag?

2019-01-03 03:02发布

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.

12条回答
我命由我不由天
2楼-- · 2019-01-03 03:10

You don't.

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, then this is down to the end-user interface, not anything you specify in the HTML. Usually, I would expect it to be based on the date format that it is set in the operating system locale settings. It makes no sense to try to override it with your own preferred format, as the format it displays in is (generally speaking) the correct one for the user's locale and the format that the user is used to writing/understanding dates in.

If you mean the format in which it's transmitted to the server, you're 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.

查看更多
成全新的幸福
3楼-- · 2019-01-03 03:11

I don't know this for sure, but I think this is supposed to be handled by the browser based on the user's date/time settings. Try setting your computer to display dates in that format.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-03 03:14

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)
查看更多
爷的心禁止访问
5楼-- · 2019-01-03 03:15

I think so, in HTML there is no syntax for DD-MM-YYYY format. Refer this page http://www.java2s.com/Code/SQLServer/Date-Timezone/FormatdateMmmddyyyyhhmmdp.htm. Somewhat it will help you. Else use javascript date picker.

查看更多
在下西门庆
6楼-- · 2019-01-03 03:15

The format of the date value 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 is to format the date in php, asp, ruby or whatever to have that format.

查看更多
再贱就再见
7楼-- · 2019-01-03 03:16

i think there is no solution if using HTML only
i think you should use jquery
the suitable jquery plugin is jQuery Masked Input

查看更多
登录 后发表回答