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

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


当前回答

要改变格式是不可能的

我们必须区分在线格式和浏览器的表示格式。

有线格式

HTML5日期输入规范引用了RFC 3339规范,该规范指定的完整日期格式等于:yyyy-mm-dd。有关更多详细信息,请参阅RFC 3339规范的5.6节。

value HTML属性和DOM属性使用这种格式,并且在进行普通表单提交时使用这种格式。

报告格式

浏览器在显示日期输入方面是不受限制的。在编写Chrome, Edge, Firefox和Opera的日期支持(见这里)。它们都显示一个日期选择器,并在输入字段中格式化文本。

桌面设备

For Chrome, Firefox, and Opera, the formatting of the input field's text is based on the browser's language setting. For Edge, it is based on the Windows language setting. Sadly, all web browsers ignore the date formatting configured in the operating system. To me this is very strange behaviour, and something to consider when using this input type. For example, Dutch users that have their operating system or browser language set to en-us will be shown 01/30/2019 instead of the format they are accustomed to: 30-01-2019.

Internet Explorer 9、10和11显示wire格式的文本输入字段。

移动设备

特别是对于Android上的Chrome,格式是基于Android显示语言的。我怀疑其他浏览器也是如此,尽管我还没能证实这一点。

其他回答

如上所述,<input type=date…>在大多数浏览器中都没有完全实现,所以让我们来谈谈webkit之类的浏览器(chrome)。

使用linux,您可以通过更改环境变量LANG, LC_TIME似乎不起作用(至少对我来说)。

您可以在终端中键入locale以查看当前值。我认为同样的理念也适用于IOS。

例如: 使用:

LANG=en_US.UTF-8 /opt/google/chrome/chrome

日期显示为mm/dd/yyyy

使用:

LANG=pt_BR /opt/google/chrome/chrome

日期显示为dd/mm/yyyy

您可以使用http://lh.2xlibre.net/locale/pt_BR/(根据您的语言环境更改pt_BR)来创建您自己的自定义语言环境并按照您想要的格式设置日期。

关于如何更改默认系统日期的一个更高级的参考是: https://ccollins.wordpress.com/2009/01/06/how-to-change-date-formats-on-ubuntu/ 而且 https://askubuntu.com/questions/21316/how-can-i-customize-a-system-locale

你可以看到你的真实当前日期格式使用date:

$ date +%x
01-06-2015

但是LC_TIME和d_fmt似乎被chrome拒绝了(我认为这是webkit或chrome中的一个bug),遗憾的是它不起作用。: ' (

所以,不幸的是,如果LANG环境变量不能解决你的问题,还没有办法。

谷歌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;
}

输出:

如前所述,官方不可能更改格式。然而,它可以对字段设置样式,因此(在一点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上失败。

要改变格式是不可能的

我们必须区分在线格式和浏览器的表示格式。

有线格式

HTML5日期输入规范引用了RFC 3339规范,该规范指定的完整日期格式等于:yyyy-mm-dd。有关更多详细信息,请参阅RFC 3339规范的5.6节。

value HTML属性和DOM属性使用这种格式,并且在进行普通表单提交时使用这种格式。

报告格式

浏览器在显示日期输入方面是不受限制的。在编写Chrome, Edge, Firefox和Opera的日期支持(见这里)。它们都显示一个日期选择器,并在输入字段中格式化文本。

桌面设备

For Chrome, Firefox, and Opera, the formatting of the input field's text is based on the browser's language setting. For Edge, it is based on the Windows language setting. Sadly, all web browsers ignore the date formatting configured in the operating system. To me this is very strange behaviour, and something to consider when using this input type. For example, Dutch users that have their operating system or browser language set to en-us will be shown 01/30/2019 instead of the format they are accustomed to: 30-01-2019.

Internet Explorer 9、10和11显示wire格式的文本输入字段。

移动设备

特别是对于Android上的Chrome,格式是基于Android显示语言的。我怀疑其他浏览器也是如此,尽管我还没能证实这一点。