默认情况下,输入type="date"显示日期为YYYY-MM-DD。
问题是,是否可能将其格式强制为:DD-MM-YYYY?
默认情况下,输入type="date"显示日期为YYYY-MM-DD。
问题是,是否可能将其格式强制为:DD-MM-YYYY?
当前回答
我知道这是一个老帖子,但它是谷歌搜索的第一个建议,简短的答案不,推荐答案用户自定义日期选择器,我使用的正确答案是使用文本框来模拟日期输入,并做任何你想要的格式,这里是代码
<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-请注意,此方法只适用于支持日期类型的浏览器。
2- JS代码中的第一个函数是针对不支持日期类型的浏览器,并将外观设置为正常的文本输入。
3-如果你将在你的页面中使用此代码进行多个日期输入,请在函数调用和输入本身中更改文本输入的ID“xTime”,当然,使用你想要的表单提交的输入名称。
4-在第二个函数中,你可以使用任何你想要的格式,而不是日+" / "+月+" / "+年,例如年+" / "+月+" / "+日,在文本输入中使用占位符或值作为页面加载时用户的yyyy / mm / dd。
其他回答
谷歌Chrome在最近的测试版中最终使用了输入type=date,格式为DD-MM-YYYY。
因此,必须有一种方法来强制使用特定的格式。我正在开发一个HTML5网页,日期搜索现在失败与不同的格式。
在阅读了大量的讨论之后,我准备了一个简单的解决方案,但我不想使用大量的Jquery和CSS,只是一些javascript。
HTML代码:
<input type="date" id="dt" onchange="mydate1();" hidden/>
<input type="text" id="ndt" onclick="mydate();" hidden />
<input type="button" Value="Date" onclick="mydate();" />
CSS代码:
#dt {
text-indent: -500px;
height: 25px;
width: 200px;
}
Javascript代码:
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;
}
输出:
如果你需要一个快速的解决方案,可以试试这个方法,让yyyy-mm-dd变成“dd- september -2016”
1)在您的输入附近创建一个span类(充当标签)
2)每次用户更改日期或需要从数据中加载时更新标签。
工作的webkit浏览器移动和指针事件的IE11+需要jQuery和jQuery日期
$("#date_input").on("change", function () { $(this).css("color", "rgba(0,0,0,0)").siblings(".datepicker_label").css({ "text-align":"center", position: "absolute",left: "10px", top:"14px",width:$(this).width()}).text($(this).val().length == 0 ? "" : ($.datepicker.formatDate($(this).attr("dateformat"), new Date($(this).val())))); }); #date_input{text-indent: -500px;height:25px; width:200px;} <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script> <input id ="date_input" dateformat="d M y" type="date"/> <span class="datepicker_label" style="pointer-events: none;"></span>
区分两种不同的格式很重要:
The RFC 3339/ISO 8601 "wire format": YYYY-MM-DD. According to the HTML5 specification, this is the format that must be used for the input's value upon form submission or when requested via the DOM API. It is locale and region independent. The format displayed by the user interface control and accepted as user input. Browser vendors are encouraged to follow the user's preferences selection. For example, on Mac OS with the region "United States" selected in the Language & Text preferences pane, Chrome 20 uses the format "m/d/yy".
HTML5规范不包括任何覆盖或手动指定这两种格式的方法。
我在两年前搜索了这个问题,我的谷歌搜索再次把我带到了这个问题。 不要浪费时间试图用纯JavaScript处理这个问题。我把时间都浪费在日日夜夜的制作上了。没有适合所有浏览器的完整解决方案。所以我建议使用momentJS / jQuery datepicker,或者告诉你的客户端使用默认的日期格式