HTML 5 - iOS上的输入类型日期格式

问题描述:

我在iPhone上使用HTML 5表单。此表单有一个输入元素,如下所示:

I am using an HTML 5 form on an iPhone. This form has an input element like the following:

<input id="birthdate" type="date" />

当用户点击此字段时,会出现iOS日期选择器。在用户选择日期后,它会将字段中的日期显示为:2012年4月14日。

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.

我的问题是,有没有办法格式化这个日期,以便在文本框中看起来像04-14-2012?或者,最好是有直接的方法将iOS日期格式转换为JavaScript Date对象吗?

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?

我一直以为格式是 YYYY-MM-DD

W3C规范& RFC3339

无论如何,你可以用JavaScript转换它。

Anyway, you could convert it with JavaScript.

var dateInput = document.getElementById('birthdate');

dateInput.addEventListener('change', function() {
    dateInput.value = new Date(dateInput.value).toISOString().split('T')[0];
}, false);