简体   繁体   English

显示默认日期值HTML表单iOS

[英]Display Default Date Value HTML Form iOS

Is there a way to display the current date inside the button which launches the date picker in a HTML form on an iOS device? 有没有一种方法可以在iOS设备上以HTML格式启动日期选择器的按钮内显示当前日期?


Here is what loads currently: 这是当前加载的内容:

空白下拉菜单

Here is what I would like it to automatically load: 这是我希望它自动加载的内容:

在此处输入图片说明


Here is the code I am using in my HTML form: 这是我在HTML表单中使用的代码:

<input name="date" type="date" autofocus="autofocus" id="date" autocomplete="on" value="09-21-13"/>

Any ideas? 有任何想法吗?

Thanks. 谢谢。

You need to find out the current date using the Date() method in JavaScript and convert it to ISO 8601 notation, yyyy-mm-dd, which is what input type=date must use internally (so the attribute value="09-21-13" is invalid and gets ignored – it would refer to day 13 in month 21 in year 9, and it is even formally invalid since the month number is too large). 您需要使用JavaScript中的Date()方法找出当前日期,并将其转换为ISO 8601表示法yyyy-mm-dd,这是input type=date必须在内部使用的(因此属性value="09-21-13"是无效的,将被忽略-它指的是第9年第21个月的第13天,由于月份数太大,因此甚至正式无效。

Example: 例:

<input name="date" type="date" autofocus="autofocus" id="date" autocomplete="on" />
<script>
function ISO8601(date) {
  var d = date.getDate();
  if(d < 10) d = '0' + d;
  var m = date.getMonth() + 1;
  if(m < 10) m = '0' + m;
  return date.getFullYear() + '-' + m + '-' + d;
}
document.getElementById('date').value = ISO8601(new Date());
</script>

If you use JavaScript libraries, some of them might have a function for converting a date to ISO 8601 notation, but the simple function above does the job, too. 如果使用JavaScript库,则其中的某些库可能具有将日期转换为ISO 8601表示法的功能,但是上面的简单函数也可以完成此工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM