Javascript 指定 HTML5 输入类型 = 日期的值输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3496241/
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
Specifying the value output of of an HTML5 input type = date?
提问by Stefan Kendall
I'd like to add native date pickers to my application, which currently uses a legacy, home-rolled system. Date input support isn't widespread, yet, but if I could present both implementations based on compatibility, that would be ideal.
我想将本机日期选择器添加到我的应用程序中,该应用程序目前使用的是传统的家庭滚动系统。日期输入支持尚不广泛,但如果我可以基于兼容性来展示这两种实现,那将是理想的。
Is there any way to specify the output of the value given by an HTML datepicker? The default for opera is yyyy-mm-dd, and I very explicitly need dd-MMM-yyyy.
有什么方法可以指定 HTML 日期选择器给出的值的输出吗?歌剧的默认值是yyyy-mm-dd,我非常明确地需要dd-MMM-yyyy.
Is there any way to control this output, currently?
目前有没有办法控制这个输出?
采纳答案by Nick
The HTML5 date inputfield is specific about the format it uses: RFC 3339 full-date
在HTML5日期输入字段是特定关于它采用以下格式: RFC 3339全日
Is it possible to change the legacy date picker to use yyyy-mm-dd format (and update the server side that reads that)?
是否可以将旧日期选择器更改为使用 yyyy-mm-dd 格式(并更新读取该格式的服务器端)?
I assume not, so then you can add some Javascript glue code that listens for a change event on the HTML5 input and updates a hidden date value field to match the format of the legacy date picker (converts yyyy-mm-dd to dd-MMM-yyyy).
我假设不是,那么您可以添加一些 Javascript 粘合代码来侦听 HTML5 输入上的更改事件并更新隐藏的日期值字段以匹配旧日期选择器的格式(将 yyyy-mm-dd 转换为 dd-MMM -yyyy)。
回答by Lg102
In Google Chrome, this has recently changed. The values displayed to the user are now based on the operating system locale. The readout, so input.value, always returns as yyyy-mm-ddregardless of the presentation format, however.
在谷歌浏览器中,这最近发生了变化。向用户显示的值现在基于操作系统区域设置。然而,无论演示格式如何,读数input.value总是返回 as yyyy-mm-dd。
Way more useful in my opinion.
在我看来更有用。
Source:
来源:
http://updates.html5rocks.com/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome
http://updates.html5rocks.com/2012/08/Quick-FAQs-on-input-type-date-in-Google-Chrome
回答by TxRegex
The format of the HTML date field is standard. While it is not possible to change this, you can easily convert from a date object to the format you are looking for given your user has JavaScript enabled.
HTML 日期字段的格式是标准的。虽然无法更改此设置,但您可以轻松地将日期对象转换为您正在查找的格式,因为您的用户已启用 JavaScript。
// Converts any valid Date string or Date object into the format dd-MMM-yyyy
function dateTransform(d) {
    var s = (new Date(d)).toString().split(' ');
    return [s[2],s[1],s[3]].join('-');
}
Otherwise you will need to submit in ISO format and implement a server-side solution. Perhaps in time, this could lead to more usage of the standard date format in every locality.
否则,您将需要以 ISO 格式提交并实施服务器端解决方案。也许随着时间的推移,这可能会导致每个地方更多地使用标准日期格式。

