Html 如何强制输入日期格式为 dd/mm/yyyy?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29873184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force the input date format to dd/mm/yyyy?
提问by Isaías Faria
I have a little problem. I am developing a web system and the form field type date is giving me a headache. The system will be for Brazil only users, then the date format should be dd/mm/yyyy. When access the site from a computer with the Portuguese language, the date of the HTML form field type works the way I want. But when access the site in a computer language English date format becomes yyyy-mm-dd. This is a problem because sometimes user does not even notice that your computer's date format is changed, but the user wants to see the date in the format dd/mm/yyyy.
我有一个小问题。我正在开发一个网络系统,表单字段类型日期让我很头疼。该系统仅适用于巴西用户,则日期格式应为 dd/mm/yyyy。从使用葡萄牙语的计算机访问该站点时,HTML 表单字段类型的日期按我想要的方式工作。但是当以计算机语言访问网站时,英文日期格式就变成了 yyyy-mm-dd。这是一个问题,因为有时用户甚至没有注意到您计算机的日期格式已更改,但用户希望以 dd/mm/yyyy 格式查看日期。
What I need is that the format is dd/mm/yyyy regardless of machine settings to access the site, and without using any additional javascript library.
我需要的是,无论访问站点的机器设置如何,格式都是 dd/mm/yyyy,并且不使用任何额外的 javascript 库。
The code used is:
使用的代码是:
<input type="date" id="date">
回答by Jimmy Chandra
No such thing. the input type=date
will pick up whatever your system default is and show that in the GUI but will always store the value in ISO format (yyyy-mm-dd). Beside be aware that not all browsers support this so it's not a good idea to depend on this input type yet.
没有这样的事。在input type=date
将拿起无论你的系统默认是和显示在图形用户界面,但将永远保存在ISO格式(YYYY-MM-DD)的值。另外请注意,并非所有浏览器都支持此功能,因此依赖此输入类型还不是一个好主意。
If this is a corporate issue, force all the computer to use local regional format (dd-mm-yyyy) and your UI will show it in this format (see wufoo link before after changing your regional settings, you need to reopen the browser).
如果是企业问题,强制所有电脑使用本地区域格式(dd-mm-yyyy),你的UI就会以这种格式显示(更改区域设置后,请查看wufoo链接,需要重新打开浏览器) .
See: http://www.wufoo.com/html5/types/4-date.htmlfor example
See: http://caniuse.com/#feat=input-datetimefor browser supports
See: https://www.w3.org/TR/2011/WD-html-markup-20110525/input.date.htmlfor spec. <- no format attr.
请参阅:http: //caniuse.com/#feat=input-datetime浏览器支持
请参阅:https: //www.w3.org/TR/2011/WD-html-markup-20110525/input.date.html了解规格。<- 没有格式属性。
Your best bet is still to use JavaScript based component that will allow you to customize this to whatever you wish.
您最好的选择仍然是使用基于 JavaScript 的组件,这将允许您根据需要对其进行自定义。
回答by Francis Ezengige
To have a constant date format irrespective of the computer settings, you must use 3 different input elements to capture day, month, and year respectively. However, you need to validate the user input to ensure that you have a valid date as shown bellow
要在不考虑计算机设置的情况下使用恒定的日期格式,您必须使用 3 个不同的输入元素分别捕获日、月和年。但是,您需要验证用户输入以确保您有一个有效的日期,如下所示
<input id="txtDay" type="text" placeholder="DD" />
<input id="txtMonth" type="text" placeholder="MM" />
<input id="txtYear" type="text" placeholder="YYYY" />
<button id="but" onclick="validateDate()">Validate</button>
function validateDate() {
var date = new Date(document.getElementById("txtYear").value, document.getElementById("txtMonth").value, document.getElementById("txtDay").value);
if (date == "Invalid Date") {
alert("jnvalid date");
}
}
回答by Shelly
DEMO : http://jsfiddle.net/shfj70qp/
演示:http: //jsfiddle.net/shfj70qp/
//dd/mm/yyyy
var date = new Date();
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
console.log(month+"/"+day+"/"+year);