javascript HTML 5 - iOS 上的输入类型日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10153492/
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
HTML 5 - Input type date formatting on iOS
提问by JavaScript Developer
I am using an HTML 5 form on an iPhone. This form has an input element like the following:
我在 iPhone 上使用 HTML 5 表单。此表单具有如下所示的输入元素:
<input id="birthdate" type="date" />
When a user clicks this field, the iOS date picker appears. After a user selects a date, it puts the date in the field displayed as something like: Apr 14, 2012.
当用户单击此字段时,会出现 iOS 日期选择器。用户选择日期后,它会将日期放在显示为以下内容的字段中:2012 年 4 月 14 日。
My question is, is there a way to format this date so that it looks like 04-14-2012 in the text box? Or, preferably, is there an straight forward approach to converting the iOS date format to a JavaScript Date object?
我的问题是,有没有办法格式化这个日期,使其在文本框中看起来像 04-14-2012?或者,最好是有直接的方法将 iOS 日期格式转换为 JavaScript 日期对象?
回答by alex
I always thought the format was meant to be YYYY-MM-DD
.
我一直认为格式应该是YYYY-MM-DD
.
Anyway, you could convert it with JavaScript.
不管怎样,你可以用 JavaScript 转换它。
var dateInput = document.getElementById('birthdate');
dateInput.addEventListener('change', function() {
dateInput.value = new Date(dateInput.value).toISOString().split('T')[0];
}, false);
回答by Kornel
No.
不。
There are two formats at play:
有两种格式在起作用:
- displayed format
- internal format exposed to JavaScript and sent to the server
- 显示格式
- 暴露给 JavaScript 并发送到服务器的内部格式
You cannot change the display format. It's up to the browser to decide how the date is presented to the user (in practice it's determined by system's locale).
您无法更改显示格式。由浏览器决定如何向用户显示日期(实际上它由系统的区域设置决定)。
You cannot change the internal format either. It's alwaysISO8601, regardless of browser/locale.
您也不能更改内部格式。无论浏览器/语言环境如何,它始终是ISO8601。
回答by karim
The following should do it.
下面应该这样做。
$dat = new DateTime($_POST['MyDATE']);
$new_format = $dat->format('Y-m-d');
回答by Tim Ferrell
I agree with @PorneL's answer, however, you can potentially do some work to simulate the results you want. I will stipulate that it's not the best way to do it.
我同意@PorneL 的回答,但是,您可以做一些工作来模拟您想要的结果。我会规定这不是最好的方法。
In pseudo code:
在伪代码中:
- Have two inputs, one of type date and another of text.
- Make the date input either invisible or 1px x 1px in side, and make the input the size that of the date that you would like
- On focus on the text input, set focus on the adjacent date input automatically.
- At this point, the user will be editing the date input field
- On change of the date field, grab the value and insert it into the text field using whatever formatting you prefer.
- 有两个输入,一个输入日期,另一个输入文本。
- 使日期输入不可见或侧边为 1px x 1px,并使输入的大小与您想要的日期大小相同
- 在文本输入的焦点上,自动将焦点设置在相邻的日期输入上。
- 此时,用户将编辑日期输入字段
- 在更改日期字段时,使用您喜欢的任何格式获取值并将其插入文本字段。
Again, it may not be the best solution, but it can work this way.
同样,它可能不是最好的解决方案,但它可以以这种方式工作。