默认情况下,输入type="date"显示日期为YYYY-MM-DD。

问题是,是否可能将其格式强制为:DD-MM-YYYY?


当前回答

自从这个问题被提出后,web领域发生了很多事情,其中最令人兴奋的是web组件的登陆。现在,您可以使用定制的HTML5元素优雅地解决这个问题,以满足您的需求。如果你想覆盖/改变任何html标签的工作方式,只需使用shadow dom构建你的。

好消息是,已经有很多可用的样板文件,所以很可能你不需要从头开始想出一个解决方案。看看人们在做什么,然后从中获得灵感。

你可以从一个简单(有效)的解决方案开始,比如聚合物的datetime-input,它允许你使用这样的标签:

 <date-input date="{{date}}" timezone="[[timezone]]"></date-input>

或者您可以获得创意和弹出完整的日期选择器样式,如您所希望的,与格式和区域设置,回调,和您的长选项列表(您有一个完整的自定义API在您的处置!)

符合标准,没有黑客。

仔细检查可用的插件,它们支持哪些浏览器/版本,以及它是否覆盖了足够百分比的用户基础……现在是2018年,所以它肯定会覆盖你的大部分用户。

希望能有所帮助!

其他回答

我知道这是一个老帖子,但它是谷歌搜索的第一个建议,简短的答案不,推荐答案用户自定义日期选择器,我使用的正确答案是使用文本框来模拟日期输入,并做任何你想要的格式,这里是代码

<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">&#9660;</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。

如前所述,官方不可能更改格式。然而,它可以对字段设置样式,因此(在一点JS的帮助下)它以我们想要的格式显示日期。这样就失去了操作日期输入的一些可能性,但如果强制使用格式的愿望更强烈,那么这种解决方案可能是一种方法。日期字段只保持这样:

<input type="date" data-date="" data-date-format="DD MMMM YYYY" value="2015-08-09">

剩下的是一些CSS和JS: http://jsfiddle.net/g7mvaosL/

$("input").on("change", function() { this.setAttribute( "data-date", moment(this.value, "YYYY-MM-DD") .format( this.getAttribute("data-date-format") ) ) }).trigger("change") input { position: relative; width: 150px; height: 20px; color: white; } input:before { position: absolute; top: 3px; left: 3px; content: attr(data-date); display: inline-block; color: black; } input::-webkit-datetime-edit, input::-webkit-inner-spin-button, input::-webkit-clear-button { display: none; } input::-webkit-calendar-picker-indicator { position: absolute; top: 3px; right: 0; color: black; opacity: 1; } <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <input type="date" data-date="" data-date-format="DD MMMM YYYY" value="2015-08-09">

它在台式机的Chrome上运行得很好,在iOS上运行得很好(尤其可取,因为在触摸屏上的本机日期操纵器是无敌的)。没有检查其他的,但是不要期望在任何Webkit上失败。

我调整了米格尔的代码,让它更容易理解 我想和像我一样有问题的人分享。

试试这个简单快捷的方法

$("#datePicker").on("change", function(e) { displayDateFormat($(this), '#datePickerLbl', $(this).val()); }); function displayDateFormat(thisElement, datePickerLblId, dateValue) { $(thisElement).css("color", "rgba(0,0,0,0)") .siblings(`${datePickerLblId}`) .css({ position: "absolute", left: "10px", top: "3px", width: $(this).width() }) .text(dateValue.length == 0 ? "" : (`${getDateFormat(new Date(dateValue))}`)); } function getDateFormat(dateValue) { let d = new Date(dateValue); // this pattern dd/mm/yyyy // you can set pattern you need let dstring = `${("0" + d.getDate()).slice(-2)}/${("0" + (d.getMonth() + 1)).slice(-2)}/${d.getFullYear()}`; return dstring; } .date-selector { position: relative; } .date-selector>input[type=date] { text-indent: -500px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="date-selector"> <input id="datePicker" class="form-control" type="date" onkeydown="return false" /> <span id="datePickerLbl" style="pointer-events: none;"></span> </div>

自从这个问题被提出后,web领域发生了很多事情,其中最令人兴奋的是web组件的登陆。现在,您可以使用定制的HTML5元素优雅地解决这个问题,以满足您的需求。如果你想覆盖/改变任何html标签的工作方式,只需使用shadow dom构建你的。

好消息是,已经有很多可用的样板文件,所以很可能你不需要从头开始想出一个解决方案。看看人们在做什么,然后从中获得灵感。

你可以从一个简单(有效)的解决方案开始,比如聚合物的datetime-input,它允许你使用这样的标签:

 <date-input date="{{date}}" timezone="[[timezone]]"></date-input>

或者您可以获得创意和弹出完整的日期选择器样式,如您所希望的,与格式和区域设置,回调,和您的长选项列表(您有一个完整的自定义API在您的处置!)

符合标准,没有黑客。

仔细检查可用的插件,它们支持哪些浏览器/版本,以及它是否覆盖了足够百分比的用户基础……现在是2018年,所以它肯定会覆盖你的大部分用户。

希望能有所帮助!

区分两种不同的格式很重要:

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规范不包括任何覆盖或手动指定这两种格式的方法。